QMap 使用详解

     QT开发之路系列文章,主要目标快速学习、完善、提升 相关技能 高效完成企业级项目开发 分享在企业中积累的实用技能和经验。

通过具体的编码过程、代码示例、步骤详解、核心内容和展示的方法解决遇到的实际问题。

转载请附上文章出处与本文链接。

QMap 使用详解目录

1 初始化

2 添加数据

3 操作符添加

4 修改元素

5 查找元素

6 删除元素

7 遍历QMap

7.1 迭代器

7.2 for循环

8 获取Value

9 多键值操作

10 源代码

11 其它文章


   

    QMap 是 Qt 框架中提供的一种关联容器类,用于存储键值对(key-value pairs)。它是一个有序的字典,允许通过键快速查找对应的值。QMap 是基于红黑树实现的,因此它的键是有序的,并且支持高效的插入、删除和查找操作。

有序性:QMap 中的键是有序的,按照键的升序排列。这意味着在遍历 QMap 时,您将按照键的顺序访问元素。

键的唯一性:每个键在 QMap 中是唯一的。如果您尝试插入一个已经存在的键,新的值将替换旧的值。

支持多种数据类型:QMap 可以使用任何可比较的类型作为键(如 int、QString、QVariant 等),并且值也可以是任何类型。

高效的查找:由于 QMap 是基于红黑树实现的,查找、插入和删除操作的平均时间复杂度为 O(log n)。

迭代器支持:QMap 提供了迭代器,可以方便地遍历键值对。

1 初始化

	QMap<QString, int> mapList;
	mapList["one"] = 1;
	mapList["two"] = 2;
	mapList["three"] = 3;
	qDebug() << "Initial map:" << mapList;

2 添加数据

	mapList.insert("one", 1);
	mapList.insert("two", 2);
	mapList.insert("three", 3);

3 操作符添加

	mapList["one"] = 1;
	mapList["two"] = 2;
	mapList["three"] = 3;

4 修改元素

	mapList["one"] = 11;
	mapList["two"] = 22;
	mapList["three"] = 33;

5 查找元素

	// 查找元素
	if (mapList.contains("one")) {
		qDebug() << "value:" << mapList.value("one");
	}

6 删除元素

	mapList.remove("one");
	qDebug() << "remove 'one':" << mapList;

7 遍历QMap

7.1 迭代器

	for (QMap<QString, int>::const_iterator it = mapList.constBegin(); it != mapList.constEnd(); ++it)
	{
		qDebug() << it.key() << ":" << it.value();
	}

7.2 for循环

	for (const auto &pair : mapList.toStdMap())
	{ 
		qDebug() << pair.first << ":" << pair.second;
	}

8 获取Value

	int value = mapList.value("one");

	QList<int>  values = mapList.values("one");

9 多键值操作

	QMultiMap<QString, int> fruitMultiMap;

	// 插入多个值
	fruitMultiMap.insert("Apple", 5);
	fruitMultiMap.insert("Banana", 3);
	fruitMultiMap.insert("Apple", 2); // 允许相同的键
	fruitMultiMap.insert("Cherry", 7);
	fruitMultiMap.insert("Banana", 4); // 另一个值与相同的键

	// 遍历 QMultiMap
	qDebug() << "Contents of QMultiMap:";
	for (auto it = fruitMultiMap.constBegin(); it != fruitMultiMap.constEnd(); ++it) {
		qDebug() << it.key() << ":" << it.value();
	}

	// 查找特定键的所有值
	QString keyToFind = "Apple";
	qDebug() << "Values for key" << keyToFind << ":";
	auto range = fruitMultiMap.equal_range(keyToFind);
	for (auto it = range.first; it != range.second; ++it) {
		qDebug() << *it; // 输出与键 "Apple" 相关的所有值
	}

	// 删除特定键的所有值
	fruitMultiMap.remove("Banana");
	qDebug() << "After removing all values for key 'Banana':";
	for (auto it = fruitMultiMap.constBegin(); it != fruitMultiMap.constEnd(); ++it) {
		qDebug() << it.key() << ":" << it.value();
	}

10 源代码

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_QMapTest.h"

#include <QMap>
#include <QList>
#include <QDebug>
#include <QMultiMap>

class QMapTest : public QMainWindow
{
    Q_OBJECT

public:
    QMapTest(QWidget *parent = nullptr);
    ~QMapTest();

private:
    Ui::QMapTestClass ui;
};



#include "QMapTest.h"

QMapTest::QMapTest(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

	QMap<QString, int> mapList;
	//mapList["one"] = 1;
	//mapList["two"] = 2;
	//mapList["three"] = 3;
	//qDebug() << "Initial map:" << mapList;


	//mapList.insert("one", 1);
	//mapList.insert("two", 2);
	//mapList.insert("three", 3);

	//mapList["one"] = 1;
	//mapList["two"] = 2;
	//mapList["three"] = 3;

	mapList["one"] = 11;
	mapList["two"] = 22;
	mapList["three"] = 33;


	//if (mapList.contains("one")) {
	//	qDebug() << "value:" << mapList.value("one");
	//}

	//mapList.remove("two");
	//qDebug() << "remove 'two':" << mapList;

	//for (QMap<QString, int>::const_iterator it = mapList.constBegin(); it != mapList.constEnd(); ++it)
	//{
	//	qDebug() << it.key() << ":" << it.value();
	//}

	//for (const auto &pair : mapList.toStdMap())
	//{ 
	//	qDebug() << pair.first << ":" << pair.second;
	//}

	//int value = mapList.value("one");

	//QList<int>  values = mapList.values("one");

	// 创建 QMultiMap
	QMultiMap<QString, int> fruitMultiMap;

	// 插入多个值
	fruitMultiMap.insert("Apple", 5);
	fruitMultiMap.insert("Banana", 3);
	fruitMultiMap.insert("Apple", 2); // 允许相同的键
	fruitMultiMap.insert("Cherry", 7);
	fruitMultiMap.insert("Banana", 4); // 另一个值与相同的键

	// 遍历 QMultiMap
	qDebug() << "Contents of QMultiMap:";
	for (auto it = fruitMultiMap.constBegin(); it != fruitMultiMap.constEnd(); ++it) {
		qDebug() << it.key() << ":" << it.value();
	}

	// 查找特定键的所有值
	QString keyToFind = "Apple";
	qDebug() << "Values for key" << keyToFind << ":";
	auto range = fruitMultiMap.equal_range(keyToFind);
	for (auto it = range.first; it != range.second; ++it) {
		qDebug() << *it; // 输出与键 "Apple" 相关的所有值
	}

	// 删除特定键的所有值
	fruitMultiMap.remove("Banana");
	qDebug() << "After removing all values for key 'Banana':";
	for (auto it = fruitMultiMap.constBegin(); it != fruitMultiMap.constEnd(); ++it) {
		qDebug() << it.key() << ":" << it.value();
	}


}

QMapTest::~QMapTest()
{}

11 其它文章

QT基本类使用详解系列文章包括以下内容:

QUUID 使用详解-CSDN博客

QDateTime 使用详解-CSDN博客

QSettings 使用详解_qt qsettings用法-CSDN博客

QFile 使用详解_qfile写文件-CSDN博客

QStringList 使用详解_qstringlist insert-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双子座断点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值