当前位置:网站首页>关于map对key自定义排序
关于map对key自定义排序
2022-07-30 05:45:00 【尹平华】
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;
}
边栏推荐
猜你喜欢
随机推荐
TDengine cluster construction
[Getting C language from zero basis - navigation summary]
Self-augmented Unpaired Image Dehazing via Density and Depth Decomposition程序运行记录
Basic application of XMLBean
Function functional interface and application
Meta分析在生态环境领域里的应用
MySQL window function
CLUE模型构建方法、模型验证及土地利用变化情景预测
联影医疗二面
二十二、Kotlin进阶学习:简单学习RecyclerView实现列表展示;
The types of data structures and MySQL index
常用损失函数(二):Dice Loss
2021-09-19 集成学习TASK2
influxDB运维记录
QT每周技巧(1)~~~~~~~~~运行图标
Knowledge distillation method of target detection
MySQL special statement and optimizer
CLUE Model Construction Method, Model Validation and Land Use Change Scenario Prediction
Redis publish/subscribe
七、Kotlin基础学习:1、创建类;2、构造函数;3、继承;4、封装;5、抽象类;6、接口;7、嵌套类;8、内部类;9、枚举类








