Qt常用容器之:QMap

一、QMap概述

QMap是Qt(跨平台应用程序开发平台)的一个关联容器,用于存储Key-Value键值对。它类似于STL的map,但有所不同。它是模板类,可以存储任意类型的数据,并且支持自定义排序规则。QMap的用法简单易懂且非常有用,它可以应用于许多领域,如图形界面、网络编程等。它可以存储任何类型的数据作为键和值,并且提供了快速查找、插入和删除操作。以下是一些常见的特性:

1.QMap是基于红黑树实现的,因此查找、插入和删除操作都具有O(log n)的时间复杂度2.QMap可以自动对键进行排序,并且支持自定义排序函数

3.QMap支持多个值对应一个键

4.支持迭代器,可以方便地遍历所有元素复制全文

二、代码示例

头文件:#include <QMap>

1.实例化QMap对象

/* 创建QMap实例, 第一个参数为QString类型的键,第二个参数为int类型的值 */

QMap<QString, int> map;

2.插入数据

/* 插入数据 两种方式*/

map["math"] = 100;

map.insert("English", 98); /* 推荐 */

打印输出: QMap((“English”, 98)(“math”, 100))

3.移除数据

/* 移除数据 */

map.remove("math");

打印输出:QMap((“English”, 98))

4.遍历数据

/* 遍历数据 (先随便插入几个)*/

map.insert("Math", 100);

map.insert("Chinese", 98);

map.insert("physical", 97);

map.insert("chemical", 96);

map.insert("biological", 95);

/* 遍历数据要使用迭代器,QT提供了两种方式的迭代 */

/* 第一种是Java类型的迭代 */

QMapIterator<QString, int> iterator(map);

while (iterator.hasNext()) {

iterator.next();

qDebug() << iterator.key() << ":" << iterator.value();

}

/* 第二种是STL类型的迭代 */

QMap<QString, int>::const_iterator iterator_1 = map.constBegin();

while (iterator_1 != map.constEnd()) {

qDebug() << iterator_1.key() << ":" << iterator_1.value();

++iterator_1;

}

/* 或 */

for (QMap<QString, int>::const_iterator it = map.constBegin(); it != map.constEnd(); it++) {

qDebug() << it.key() << ": " << it.value();

}

打印输出:两种方法输出一样

“Chinese” : 98

“English” : 99

“Math” : 100

“biological” : 95

“chemical” : 96

“physical” : 97

5.由键查找对应键值

map.value("Math");

打印输出:100

6.由键值查找键

QString k = map.key(100);

qDebug() << k;

打印输出:“Math”

7.修改键值

/* 通常一个键只对应一个值,如果再次调用insert()方法,会覆盖以前的值 */

map.insert("Math", 120);

qDebug() << map.value("Math");

打印输出:120

8.查找是否包含某个键

bool isok = map.contains("Math");

qDebug() << isok;

打印输出:true

9.获取所有的键和键值

QList<QString> allKeys = map.keys();

qDebug() << allKeys;

QList<int> allValues = map.values();

qDebug() << allValues;

打印输出:

(“Chinese”, “English”, “Math”, “biological”, “chemical”, “physical”)

(98, 99, 120, 95, 96, 97)

10.清除数据

map.clear();

qDebug() << map.isEmpty();

打印输出:true

11.一个键对应多个值

方式一:通过insertMulti方法

/* 通过insert方法进行插值会覆盖以前的值,但是通过insertMulti方法不会覆盖,而是会增加一对 */

map.insert("Math", 100);

map.insertMulti("Math", 150);

qDebug() << map.value("Math");

qDebug() << map.values("Math");/* 获取Math所有键值 */

/* 查看当前键和键值的数量 */

qDebug() << map.keys().size();

qDebug() << map.values().size();

打印输出:150

(150, 100)

2

2

总结:通过insertMulti方法可以使得一个键对应多个键值,通过value获取其最后一次插入得键值,通过values获取其所有键值。

注意这一点与STL中得map不同,在STL中,map对象是不允许有多个相同键的

/* 这是STL测试用例 */

#include <iostream>

#include <map>

#include <string>

using namespace std;

int main()

{

map<string, int> mp;

mp.insert(pair<string, int>("Math", 100));

/* 再次插入一个键为"Math"的map */

mp.insert(make_pair("Math",120));

for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++)

{

cout << "key = " << it->first << " value: " << it->second << endl;

}

return 0;

}

打印输出:key = Math value: 100

虽然插入了两次,但是因为都是同一个键,故第二次插入无效。

方式二:通过QMultiMap类

map.insert("Math", 100);

map.insertMulti("Math", 150);

map.insertMulti("Math", 120);

qDebug() << map.values("Math");

QMultiMap<QString, int> multiMap;

multiMap.insert("Math", 100);

multiMap.insert("Math", 90);

multiMap.insert("Math", 80);

qDebug() << multiMap.values("Math");

打印输出:

(120, 150, 100)

(80, 90, 100)

两种方法效果是一样的。

QMultiMap遍历数据

/* 遍历全部 */

for (QMultiMap<QString, int>::iterator it = multiMap.begin(); it != multiMap.end(); it++) {

qDebug() << it.key() << ": " << it.value();

}

/* 只遍历指定键 */

for (QMultiMap<QString, int>::iterator it = multiMap.find("Math"); it != multiMap.end(); it++) {

qDebug() << it.key() << ": " << it.value();

}

三、QMap的局限性

QMap的主要局限性在于内存开销问题。由于是一个红黑树结构,每个元素都需要一个节点来存储,所以它的空间复杂度是O(n)。同时,由于红黑树本身需要进行平衡操作,

所以在某些情况下,效率不如散列表。因此,在选择数据结构时,需要考虑到应用的场景以及数据规模。如果需要进行大量的键值对的快速查询、插入、删除操作,可以使用散列表,否则,可以选择使用QMap。

散列表(Hash Table,也叫哈希表)是一种数据结构,Qhash使用哈希函数将键映射到桶(bucket)中,以实现快速查找、插入和删除操作。散列表的主要优点是查找速度快,平均时间复杂度为 O(1)。在遍历QHash时,元素的顺序是任意的。在QMap中,元素总是按键排序了的。

  • 18
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值