#include<iostream>
#include<deque>
#include<vector>
#include<stack>
#include<algorithm>
#include<Queue>
#include<ctime>
#include<list>
using namespace std;
//list容器的排序案例 对于自定义的数据类型做排序
//按照年龄进行升序,如果年龄相同按照身高进行排序
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;
};
void printList(const list<Person>&L)
{
for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout<<"name: " << (*it).m_Name << " age: "<<(*it).m_Age<<" height: "<<(*it).m_Height;
cout << endl;
}
}
//指定排序规则
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("a", 18,160);
Person P2("f", 38,169);
Person P3("b", 38,172);
Person P4("d", 38,158);
Person P5("h", 88,188);
Person P6("c", 28,135);
L.push_back(P1);
L.push_back(P2);
L.push_back(P3);
L.push_back(P4);
L.push_back(P5);
L.push_back(P6);
cout << "排序之前:" << endl;
printList(L);
cout << "----------------------" << endl;
cout << "排序之后: " << endl;
L.sort(ComparePerson);
printList(L);
}
int main()
{
test01();
//test02();
system("pause");
return 0;
}