map对key默认是从小到大排序
也可以自定义排序
#include <iostream>
#include <map>
#include <string>
// 定义自己std::map比较器
template<class _Ty>
struct PLess
{
// functor for operator<
bool operator()(const _Ty& pLeft, const _Ty& pRight) const
{
// apply operator< to operands
return pLeft > pRight; // 这个比较器与默认比较器不同的地方
}
};
int main()
{
// 如果Key是一个指针, 为了正确排序, 需要自己定义比较器对象
std::map<int, int, PLess<int>> datas;
datas[1] = 3;
datas[2] = 2;
datas[3] = 1;
return 0;
}