代码语言:javascript复制
#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;
}
总结: