vector按存储的结构体的某一字段排序

include

include

struct Person
{
string Name;
string Sex;
int Age;
int High;
Person()
:Age(0)
,High(0)
{
}
};

//定义比较函数
bool CmpAge(const Person& p1,const Person& p2)
{
return p1.Age < p2.Age;//小于号表示:从小到大排序(小的在前,大的在后)
//大于号相反
}

Person per1,per2;
per1.Name = “xiaochun”;
per1.Sex = “男”;
per1.Age = 21;
per1.High = 168;

per2.Name = “chunxiao”;
per2.Sex = “男”;
per2.Age = 22;
per2.High = 168;

vector vec_Person;
vec_Person.pushback(per1);
vec_Person.pushback(per2);

//排序
sort(vec_Person.begin(),vec_Person.end(),CmpAge);

//vector输出
//省略
……

你可能感兴趣的:(vector按存储的结构体的某一字段排序)