排序算法
案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高
排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序
代码如下:
创建list容器.cpp文件
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<list>
using namespace std;
class Person
{
public:
Person(string name,int age,int height)
{
this->m_Age = age;
this->m_Height = height;
this->m_Name = name;
}
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 << " 姓名: " << it->m_Name
<< " 年龄: " << it->m_Age
<< " 身高: " << it->m_Height << endl;