//默认排序规则为:按照key值,从小到大,掌握如何改变排序规则
//利用仿函数改变排序规则
#include<iostream>
#include<map>
using namespace std;
class MyCompare {
public:
bool operator()(int v1, int v2)const {
//降序
return v1 > v2;
}
};
void test01() {
map<int, int,MyCompare>m;
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 30));
m.insert(pair<int, int>(3, 40));
for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "key = " << (*it).first << " value = " << it->second << endl;
}
cout << endl;
}
int main() {
test01();
}
c++笔记 STL map容器_排序
最新推荐文章于 2022-07-08 23:23:29 发布