网站首页 > 博客文章 正文
QMap 是 Qt 库中的一个映射容器,用于存储键/值对。它是 STL 中的 std::map 的替代品,并且具有更好的性能和可读性。
使用 QMap 可以方便地对键/值对进行插入、删除和查找操作,同时保证了键的唯一性。
语法:
QMap<Key, T> map;
其中,Key 为键的数据类型,T 为值的数据类型。
以下是 QMap 的常用操作:
- 插入键/值对:
map.insert(key, value);
- 删除键/值对:
map.remove(key);
- 查找键/值对:
T value = map[key];
- 判断键是否存在:
if (map.contains(key)) {
// 键存在
}
示例代码:
#include <QMap>
#include <QDebug>
int main() {
QMap<QString, int> map;
// 插入键/值对
map.insert("John", 25);
map.insert("Jane", 30);
map.insert("Jim", 35);
// 查找键/值对
int age = map["John"];
qDebug() << "John's age is" << age;
// 判断键是否存在
if (map.contains("Jane")) {
qDebug() << "Jane exists in the map.";
}
return 0;
}
输出结果:
John's age is 25
Jane exists in the map.
总结:QMap 是 Qt 库中一个非常方便的映射容器,能够实现简单易用的键/值对操作。
猜你喜欢
- 2025-06-13 只需在注册表中新建一个值,就可以禁用键盘上的大写锁定键
- 2025-06-13 MapStruct架构设计(maps模块)
- 2025-06-13 轻松解读源码系列之Java集合接口&抽象类(1)—Map和Collection
- 2025-06-13 宇宙厂:WeakMap 和 WeakSet 和垃圾回收器有什么关系?
- 2025-06-13 Seata源码—4.全局事务拦截与开启事务处理二
- 2025-06-13 rust map与c++区别(rust c++ 性能比较)
- 2025-06-13 「吐血整理」想学Google Guava看这篇就够了
- 2025-06-13 Map的几种遍历方式(map的三种遍历方式js)
- 2025-06-13 Go语言映射(Map)类型详解(go中的map)
- 2025-06-13 golang笔试题(golang面试参考手册)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)