#include<iostream>
using namespace std;
#include<map>
#include<string>
class person
{
public:
person(string name, int age,int height,int weight)
{
this->mname = name;
this->mage = age;
this->mheight = height;
this->mweight = weight;
}
string mname;
int mage;
int mheight;
int mweight;
};
class compareperson
{
public:
bool operator()(const person& p1, const person& p2)
{
if (p1.mage == p2.mage)
{
if (p1.mheight == p2.mheight)
{
return p1.mweight > p2.mweight;
return p1.mheight > p2.mheight;
}
}
else
{
return p1.mage > p2.mage;
}
}
};
void test01()
{
map<int,person>m;
person p1("曹操", 35, 175, 140);
person p2("刘备", 45, 180, 150);
person p3("孙权", 40, 170, 140);
person p4("赵云", 25, 190, 160);
person p5("张飞", 35, 175, 180);
person p6("关羽", 35, 200, 170);
m.insert(make_pair(1, p1));
m.insert(make_pair(2, p2));
m.insert(make_pair(3, p3));
m.insert(make_pair(4, p4));
m.insert(make_pair(5, p5));
m.insert(make_pair(6, p6));
map<int, person, compareperson>m;
for (map<int, person, compareperson>::iterator it = m.begin(); it != m.end(); it++)
{
????????????????????????????????????
}
}
int main()
{
test01();
system("pause");
return 0;
}
it->first it->second 分别对应key value
第三个参数是你的排序规则,你这里就是正常的迭代器遍历,
直接
cout<<it->first<<it->second<<endl;
就行了,for循环里的it++;就理解为依次去访问指向的元素,begin到end就相当于遍历了。
C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html
C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html