写了一串代码,利用仿函数想让map里面降序排列,也是按照网上教程写的,但是我的结果还是升序的:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class MyCompare {
public:
bool operator()(int v1, int v2)const {
return v1 > v2;
}
};
int main() {
map<int, string>mp;
mp.insert(pair<int , string>(1, "张三"));
mp.insert(pair<int , string>(2, "李四"));
mp.insert(pair<int , string>(4, "王五"));
mp.insert(pair<int , string>(3, "田七"));
for (map<int, string, MyCompare>::iterator it = mp.begin(); it != mp.end(); it++) {
cout << (*it).first << " " << (*it).second << endl;
}cout << endl;
return 0;
}
运行结果如图: