#include<iostream>
using namespace std;
#include<list>
#include<string>
//创建模板
class Person
{
public:
Person(string name, int age, int high)
{
m_name = name;
m_age = age;
m_high = high;
}
public:
string m_name;
int m_age;
int m_high;
};
bool Compare(Person& p1, Person& p2)
{
if (p1.m_age == p2.m_age)
{
return p1.m_high>p2.m_high;
}
else
{
return p1.m_age > p2.m_age;
}
}
int main()
{
Person p1("刘备", 20, 150);
Person p2("关羽", 20, 170);
Person p3("张飞", 20, 190);
//创建容器 存类
list<Person>v;
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
//比较函数 重新构造
v.sort(Compare);
for (list<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "名字:" << it->m_name << " " << "年龄:" << it->m_age << " "
<< "身高为:" << it->m_high << endl;
}
}
如有错误,多多指教