设计作品展示网站,宣传片拍摄报价明细,凡科建站登录入口官方正版,模板网站建设乐云seo效果好QMap使用详解 1. 实例化 QMap 对象2. 插入数据3. 移除数据4. 遍历数据5. 由键查找对应键值6. 由键值查找键7. 修改键值8. 查找是否包含某个键9. 获取所有的键和键值10.清除数据11.一个键对应多个值12.QMultiMap 遍历数据13.完整示例代码14.使用自定义键类型的 QMap示例#xf… QMap使用详解 1. 实例化 QMap 对象2. 插入数据3. 移除数据4. 遍历数据5. 由键查找对应键值6. 由键值查找键7. 修改键值8. 查找是否包含某个键9. 获取所有的键和键值10.清除数据11.一个键对应多个值12.QMultiMap 遍历数据13.完整示例代码14.使用自定义键类型的 QMap示例使用自定义键类型的 QMap步骤 1定义自定义键类型步骤 2将自定义键类型用于 QMap QMap 是 Qt 提供的一个模板类用于存储键值对。默认情况下
QMap 可以使用基本数据类型或 Qt 的基本类型如
QString,
int,
QDate 等作为键。但是如果你需要使用自定义类型作为键则需要满足一些特定的条件。 以下是使用自定义键开发 QMap 的详解
1. 实例化 QMap 对象
QMapint, QString map;2. 插入数据
map.insert(1, One);
map.insert(2, Two);
map.insert(3, Three);3. 移除数据
map.remove(2); // 移除键为 2 的项4. 遍历数据
for (auto it map.begin(); it ! map.end(); it) {qDebug() Key: it.key() Value: it.value();
}5. 由键查找对应键值
if (map.contains(1)) {QString value map.value(1);qDebug() Value for key 1: value;
}6. 由键值查找键
QMap 本身没有直接提供由键值查找键的功能但可以通过遍历来实现
int key -1;
QString targetValue Three;
for (auto it map.begin(); it ! map.end(); it) {if (it.value() targetValue) {key it.key();break;}
}
qDebug() Key for value Three: key;7. 修改键值
map[1] Uno;8. 查找是否包含某个键
bool containsKey map.contains(1);
qDebug() Contains key 1: containsKey;9. 获取所有的键和键值
QListint keys map.keys();
QListQString values map.values();qDebug() Keys: keys;
qDebug() Values: values;10.清除数据
map.clear();11.一个键对应多个值
对于一个键对应多个值的需求可以使用 QMultiMap。
QMultiMapint, QString multiMap;
multiMap.insert(1, One);
multiMap.insert(1, Uno);
multiMap.insert(2, Two);// 遍历 QMultiMap 数据
for (auto it multiMap.begin(); it ! multiMap.end(); it) {qDebug() Key: it.key() Value: it.value();
}// 获取所有值为某键的列表
QListQString valuesForKey multiMap.values(1);
qDebug() Values for key 1: valuesForKey;12.QMultiMap 遍历数据
for (auto it multiMap.begin(); it ! multiMap.end(); it) {qDebug() Key: it.key() Value: it.value();
}13.完整示例代码
以下是一个完整的例子将上述所有操作整合在一起
#include QCoreApplication
#include QMap
#include QMultiMap
#include QDebugint main(int argc, char *argv[]) {QCoreApplication a(argc, argv);// 实例化 QMap 对象QMapint, QString map;// 插入数据map.insert(1, One);map.insert(2, Two);map.insert(3, Three);// 移除数据map.remove(2); // 移除键为 2 的项// 遍历数据for (auto it map.begin(); it ! map.end(); it) {qDebug() Key: it.key() Value: it.value();}// 由键查找对应键值if (map.contains(1)) {QString value map.value(1);qDebug() Value for key 1: value;}// 由键值查找键int key -1;QString targetValue Three;for (auto it map.begin(); it ! map.end(); it) {if (it.value() targetValue) {key it.key();break;}}qDebug() Key for value Three: key;// 修改键值map[1] Uno;// 查找是否包含某个键bool containsKey map.contains(1);qDebug() Contains key 1: containsKey;// 获取所有的键和键值QListint keys map.keys();QListQString values map.values();qDebug() Keys: keys;qDebug() Values: values;// 清除数据map.clear();// 一个键对应多个值QMultiMapint, QString multiMap;multiMap.insert(1, One);multiMap.insert(1, Uno);multiMap.insert(2, Two);// 遍历 QMultiMap 数据for (auto it multiMap.begin(); it ! multiMap.end(); it) {qDebug() Key: it.key() Value: it.value();}// 获取所有值为某键的列表QListQString valuesForKey multiMap.values(1);qDebug() Values for key 1: valuesForKey;return a.exec();
}这段代码展示了如何实例化 QMap 和 QMultiMap 对象插入、移除、遍历数据查找键和值修改键值清除数据并处理一个键对应多个值的情况。
14.使用自定义键类型的 QMap 自定义键类型需要实现以下操作符 运算符用于比较两个键是否相等。 运算符用于比较两个键的大小。QMap 内部使用此操作符来保持元素的顺序。 自定义键类型需要支持复制和赋值。
示例使用自定义键类型的 QMap
假设你有一个自定义类型 Person包含 firstName 和 lastName 字段你希望将 Person 作为 QMap 的键。
步骤 1定义自定义键类型
#include QString
#include QDebugclass Person {
public:QString firstName;QString lastName;Person() {}Person(const QString first, const QString last) : firstName(first), lastName(last) {}bool operator(const Person other) const {return firstName other.firstName lastName other.lastName;}bool operator(const Person other) const {if (lastName other.lastName) {return firstName other.firstName;}return lastName other.lastName;}
};inline uint qHash(const Person key, uint seed) {return qHash(key.firstName, seed) ^ qHash(key.lastName, seed);
}步骤 2将自定义键类型用于 QMap
#include QMap
#include QDebugint main() {QMapPerson, int peopleMap;// 添加元素到 QMappeopleMap.insert(Person(John, Doe), 30);peopleMap.insert(Person(Jane, Doe), 25);peopleMap.insert(Person(Alice, Smith), 28);// 遍历 QMapfor (auto it peopleMap.begin(); it ! peopleMap.end(); it) {qDebug() it.key().firstName it.key().lastName : it.value();}return 0;
}