zl程序教程

您现在的位置是:首页 >  后端

当前栏目

map容器排序

2023-09-14 09:02:34 时间

在这里插入图片描述

#include<iostream>
using namespace std;
#include<map>
class person {
public:
	person(int id,int s,int m):id(id),salary(s),moneyBag(m){}
	int id;   //员工编号
	int salary;  //工资
	int  moneyBag;  //分红
};
//自定义排序规则
class compare {
public:
	bool operator()(int v1, int v2) const
	{
		return v1 > v2;
	}
};
void print(const map<int,person,compare>& m)
{
	for (map<int,person,compare>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "员工编号:  " << (*it).second.id << "    员工工资:  " << (*it).second.salary << "   员工的分红: " << (*it).second.moneyBag << endl;
	}
}
void test()
{
	person w1(01, 12000, 8000);
	int sum1 = w1.salary + w1.moneyBag;
	person w2(02, 19000, 10000);
	int sum2 = w2.salary + w2.moneyBag;
	person w3(03, 20000, 12000);
	int sum3 = w3.salary + w3.moneyBag;
    //自定义数据类型排序
	//按照总薪酬,从大到小排序
	map<int, person,compare> m;
	m.insert(make_pair(sum3, w3));
	m.insert(make_pair(sum1, w1));
	m.insert(make_pair(sum2, w2));   
	print(m);
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

总结:
在这里插入图片描述