当前位置:网站首页>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();
}
調試截圖如下:

源碼打包下載地址:
边栏推荐
- Purchase guide - how to purchase a high-quality conference tablet, these aspects must be compared
- [C language] deeply analyze the relationship between pointer and array
- 交互电子白板有哪些特点?电子白板功能介绍
- Short video source code development, high-quality short video source code need to do what?
- mysql5.7.27安装之windows8.1 64
- What are the characteristics of the interactive whiteboard? Function introduction of electronic whiteboard
- Redis实现延迟队列的正确姿势
- 招行23型号UKey在win7上无法识别
- Spark性能调优之道——解决Spark数据倾斜(Data Skew)的N种姿势
- Shell learning
猜你喜欢

Summary of JS methods for obtaining data types

Short video source code development, high-quality short video source code need to do what?

Gridhome, a must-have static site generator for beginners
Database mysql master-slave scheme

【C语言深度解剖】关键字if&&else&&bool类型

Learning about ABAP program tuning (IV) loop where key

System throughput, TPS (QPS), user concurrency, performance test concepts and formulas

面对默认导入失败的情况

新手必会的静态站点生成器——Gridsome

【C语言】深度剖析整型和浮点型在内存中的存储
随机推荐
Idea installation summary
The world's "first" IEEE privacy computing "connectivity" international standard led by insight technology was officially launched
Spark's NaiveBayes Chinese text classification
同花顺怎么开户?网上开户安全么?
数据库mysql 主从方案
洞见科技牵头的全球「首个」IEEE隐私计算「互联互通」国际标准正式启动
linux系统维护篇:mysql8.0.13源码下载及安装之“傻瓜式”操作步骤(linux-centos6.8)亲测可用系列
In the era of video explosion, who is supporting the high-speed operation of video ecological network?
短视频源码开发,优质的短视频源码需要做好哪几点?
Interface idempotent design
5 modes of IO model
Basic application of scala for
[pop up box 2 at the bottom of wechat applet package]
Vhedt business development framework
华为云招募工业智能领域合作伙伴,强力扶持+商业变现
【C语言】库函数qsort的使用
Summary of spark common operators
双向数据绑定v-model与v-decorator
Purchase guide - how to purchase a high-quality conference tablet, these aspects must be compared
【C语言深度解剖】关键字if&&else&&bool类型
https://github.com/fengfanchen/Qt/tree/master/QMapKeyCustom