# include<iostream>
# include<list>
using namespace std;
class Person
{
public:
Person(string name, int age, int height) {
this->m_name = name;
this->m_age = age;
this->m_height = height;
}
string m_name;
int m_age;
int m_height;
};
//指定排序规则
bool comparePerson(Person &p1,Person &p2)
{
//按照年龄做一个升序
if (p1.m_age == p2.m_age)
return p1.m_height > p2.m_height;
else
return p1.m_age < p2.m_age;
}
void test01()
{
//所有不支持随机访问迭代器的容器,不可以使用标准算法
//不支持随机访问迭代器的容器,内部会供应对应的一些算法
list<Person>L;
Person p1("aa",35,160);
Person p2("bb", 35, 167);
Person p3("cc", 35, 157);
Person p4("dd", 39, 185);
Person p5("ee", 38, 176);
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
{
cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << " 身高:" << it->m_height << endl;
}
//排序后
cout<< "----------------------------------" << endl;
L.sort(comparePerson);
for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
{
cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << " 身高:" << it->m_height << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}