C 對存放物件的Vector容器按自定義字段排序

2021-08-03 09:48:23 字數 1120 閱讀 6163

此處定義結構體或者類都一樣,都可以借助演算法庫,自己定義排序規則,然後使用sort方法,靈活變通

student.h

#ifndef student_h

#define student_h

#endif

class student;

student.cpp

#include 

#include "student.h"

using

namespace

std;

student::student(){}

student::student(int s_age,string s_name)

student::~student()

main.cpp

#include 

#include "student.h"

#include

#include

#include

using

namespace

std;

//年齡逆序

bool negative_sort_age(student s1,student s2)

//年齡正序

bool positive_sort_age(student s1,student s2)

//姓名逆序

bool negative_sort_name(student s1,student s2)

//姓名正序

bool positive_sort_name(student s1,student s2)

//列印student物件

void print_stu(student &stu)

//列印存放student物件的vector容器

void print_v_stu(vector

v)}int main(int argc, char

const *argv)

容器(容器中只能存放物件)

arraylist 本質是object陣列 private transient object elementdata 預設陣列的長度為10 add時超過10則陣列長度增加原來長度的一半 list變陣列 t list.toarray newt list.size linkedlist 本質是雙向的鍊錶...

vector中存放物件和指標的區別

這裡先說出結論 vector中push back物件時,會呼叫物件的拷貝建構函式。而且在vector空間不足時,繼續push back,vector會將之前的所有物件都拷貝構造到一塊更大的空間裡。也就是說物件如果較大,那麼最好用vector儲存指標以減少呼叫拷貝構造 造成的消耗,如果vector存指...

對存放物件的陣列進行去重

const arr 比如上面這個資料,我們要對arr中的value進行比較,去掉value重複的物件元素,去重的思想就是,我們要新建乙個物件用來存放arr中的value這個屬性的值 將arr中的value值作為新建立的物件的屬性 後面就判斷這個屬性有沒有存在,如果沒有存在,就將元素新增到新陣列,並且...