list排序

#include <iostream>
#include <string>
#include <fstream>//包含头文件 写文本
#include <vector>
#include <algorithm>
#include <deque>
#include<list>
using namespace std;
//显示数据
class person
{
public:
	person(string name,int age, int tall)
	{
		this->name = name;
		this->age = age;
		this->tall = tall;
	}
	string name;
	int age;
	int tall;
};
void test01(list<person>& l)
{
	for (list<person>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << (*it).name << " " << it->age << " " << it->tall << " " << endl;
	}
	cout << endl;
}
bool compare(person& p1,person&p2)
{
	if (p1.age==p2.age)
	{
		return p1.tall > p2.tall;
    } 
	else
	{
		return p1.age < p2.age;

	}
	
}
//bool compare(int a, int b)
//{
//	return a > b;
//}
//list函数构造
//+数据
void test()
{
	person p1("李四", 20, 198);
	person p2("的撒", 60, 200);
	person p3("木子", 25, 160);
	person p4("木木", 25, 162);
	person p5("阿飞", 25, 163);
	person p6("小新", 50, 175);
	list<person>l1;
	l1.push_back(p1);
	l1.push_back(p2);
	l1.push_back(p3);
	l1.push_back(p4);
	l1.push_back(p5);
	l1.push_back(p6);
	test01(l1);
	l1.sort(compare);
	test01(l1);
	//排序

	
}

int main() {
	test();
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值