当前位置:网站首页>Qt筆記-QMap自定義鍵(key)
Qt筆記-QMap自定義鍵(key)
2022-06-22 16:58:00 【IT1995】
如下key值:
struct MyKey{
MyKey(QString key1, int key2){
this->key1 = key1;
this->key2 = key2;
}
QString key1;
int key2;
};自定義key時會出現如下問題:

需要重寫左操作符號,如下:
struct MyKey{
MyKey(QString key1, int key2){
this->key1 = key1;
this->key2 = key2;
}
bool operator < (const MyKey &key) const{
return std::tie(key1, key2) < std::tie(key.key1, key.key2);
}
QString key1;
int key2;
};所有源碼如下:
#include <QCoreApplication>
#include <QMap>
#include <QDebug>
struct MyKey{
MyKey(QString key1, int key2){
this->key1 = key1;
this->key2 = key2;
}
bool operator < (const MyKey &key) const{
return std::tie(key1, key2) < std::tie(key.key1, key.key2);
}
QString key1;
int key2;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMap<MyKey, QString> map;
map.insert(MyKey("10086", 0), "value1");
map.insert(MyKey("10086", 1), "value2");
map.insert(MyKey("10086", 2), "value3");
map.insert(MyKey("10010", 1), "value4");
map.insert(MyKey("10010", 2), "value5");
return a.exec();
}
調試截圖如下:

源碼打包下載地址:
边栏推荐
- Spark's NaiveBayes Chinese text classification
- 【微信小程序获取自定义tabbar的高度】绝对可用!!!
- 同花顺是什么?在线开户安全么?
- ERROR 1364 (HY000): Field ssl_cipher doesnt have a default value
- LETV group payment system architecture sharing for processing 100000 high concurrent orders per second
- The world's "first" IEEE privacy computing "connectivity" international standard led by insight technology was officially launched
- jsp學習之(二)---------jsp脚本元素和指令
- 【C语言】深度剖析整型和浮点型在内存中的存储
- What should I do if I can't hear a sound during a video conference?
- Add a millennial sign to a number (amount in millennia)
猜你喜欢
Database mysql master-slave scheme

Linux system maintenance: mysql8.0.13 source code download and installation "fool" operation steps (Linux centos6.8) test available series

社会担当 广汽本田“梦想童行”倡导儿童道路交通安全

系统吞吐量、TPS(QPS)、用户并发量、性能测试概念和公式
![[Alibaba cloud server - install MySQL version 5.6 and reinstall]](/img/5a/50b1de5f58235f6d11f6ad1eecc455.png)
[Alibaba cloud server - install MySQL version 5.6 and reinstall]

短视频源码开发,优质的短视频源码需要做好哪几点?

2022年中国重卡智能化升级专题研究

How to add a "security lock" to the mobile office of government and enterprises?

【C语言】库函数qsort的使用

【阿里云服务器-安装mysql的5.6版本安装,重装】
随机推荐
迭代器与生成器
新手必会的静态站点生成器——Gridsome
mysql账号增删改、数据导入导出命令举例
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
Oracle database and table
Analysis of the read data source code of spark shuffle
图计算Hama-BSP模型的运行流程
交互电子白板有哪些特点?电子白板功能介绍
[Alibaba cloud server - install MySQL version 5.6 and reinstall]
How to add a "security lock" to the mobile office of government and enterprises?
mysql5.7.27安装之windows8.1 64
从Application提交角度审视Executor
Jsp Learning (2) - - jsp script Elements and instructions
Redis实现延迟队列的正确姿势
[wechat applet custom bottom tabbar]
Shell learning
【游标的嵌套】mysql存储过程游标的嵌套
【阿里云服务器-安装mysql的5.6版本安装,重装】
接口(优化类型注解)
Summary of spark common operators
https://github.com/fengfanchen/Qt/tree/master/QMapKeyCustom