关于map对key自定义排序
生活随笔
收集整理的這篇文章主要介紹了
关于map对key自定义排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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;
}
?
總結
以上是生活随笔為你收集整理的关于map对key自定义排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows巡检IIS指标脚本(Pow
- 下一篇: OpenGL 入门第一课 视窗以及三角形