map容器默认从小到大排序
利用仿函数可以修改map容器的排序规则为从大到小
示例:
#include<iostream>
#include<string>
#include<map>
using namespace std;
class MyCompare
{
public:
bool operator()(const int v1, const int v2) const {
return v1 > v2;
}
};
void test01() {
map<int, int, MyCompare>m;
m.insert(make_pair(7, 30));
m.insert(make_pair(3, 30));
m.insert(make_pair(5, 60));
for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end();it++) {
cout << "key: " << it->first << " value: " << it->second << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}