当前位置:网站首页>Qt 容器类
Qt 容器类
2022-06-29 06:37:00 【Mr.codeee】
1.概述
Qt提供了多个基于模板的容器类,这些类可以用于存储指定类型的数据项,例如QStringList就是从QList<QString>继承的,可以实现对字符串的增、删等操作。
Qt的容器类分为顺序容器和关联容器。
Qt的容器类比STL更轻巧、安全和容易使用,并且还是线程安全的。
例:定义一个QList<QString> 容器
QList<QString> str;
str.append("A");
str.append("B");
str.append("C");2.顺序容器类
Qt顺序容器类如下所示:
- QList
- QLinkedList
- QVector
- QStack
- QQueue
2.1QList
比较常用的容器类,以数组列表的形式实现,在前、后添加数据非常快。以下为常用方法。
插入:insert()
删除:removeAt()
替换:replace()
移动:move()
添加:append()
2.2QLinkedList
是链式列表,数据项不是连续的内存存储,基于迭代器访问数据项,插入和删除数据项操作时间相同
2.3QVector
提供动态数组的功能,与QList接口基本相同,数据项是连续存储的。
2.4QStack
类似于堆栈,后入先出的特点,push()和pop()用于数据进出栈。
QStack<int> stack;
stack.push(1);
stack.push(2);
stack.push(3);
while(!stack.isEmpty())
stack.pop();2.5QQueue
类似于队列,先入先出的特点,enqueue()和dequeue()用于操作数据进出队列。
QQueue<int> queue;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
while(!queue.isEmpty())
queue.dequeue();3.关联容器类
关联容器如下所示
- QMap
- QMultiMap
- QHash
- QMultiHash
- QSet
3.1QSet
基于散列表的集合模板类,存储数据的顺序不定,查找速度非常快。
3.2QMap
QMap存储数据按照键的顺序来存储的,一个键映射一个值。
QMap<int,int> map;
map[1] = 1;
map[2] = 2;
map[3] = 3;
//或者使用insert
QMap<int,int> map;
map.insert(1,1);
map.insert(2,2);
map.insert(3,3);查找
int num = map[1];
//或者
int num2 = map.value[2];3.3QMultiMap
是QMap的子类,一个键可以对应多个值。
QMultiMap<int,int> map;
map.insert(1,1);
map.insert(1,2);
//map.size() == 2 3.4QHash
基于散列表来实现的,查找速度非常快。
和QMap比较
- QHash查找速度更快
- QMap是按键顺序排序的,QHash数据项任意排序
3.5QMultiHash
QMultiHash是QHash的子类,用于处理多值映射的类,与QMultiMap类似。
边栏推荐
- Is there any difference between a=a+b and a+=b?
- What should I learn before learning programming?
- [C language] flexible array
- Labor skills courses integrated into steam Education
- [MySQL technology topic] technical analysis and guide for analyzing the high availability architecture of MySQL
- 【OSPF引入直连路由时巧借静态黑洞路由做汇总】
- Qt 程序打包发布-windeployqt工具
- List集合实现分页
- How to fix Error: Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorli
- centos下配置mysql 5.7 和 8
猜你喜欢

The echares map is implemented separately by provinces, and the tooltip user-defined prompt box, scattered annotation and scattered illumination are explained in detail

Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- multi axis mirroring function

json tobean

Redistemplate handles hash integer type problem resolution

Move disassembly of exclusive delivery of script (the first time)
![[C language] flexible array](/img/22/3255740602232abfdf69624762adca.jpg)
[C language] flexible array

Print Yanghui triangle

jetson tx2

Analytic hierarchy process

How does schedulerx help users solve distributed task scheduling problems?
随机推荐
Open source 23 things shardingsphere and database mesh have to say
Fault: ntfrs warning log for id13562
Single application and microservice application
National Defense University project summary
UVM验证平台
2022.02.14
flutter配置国内镜像,连接真机
Is there any difference between a=a+b and a+=b?
Li Kou today's question -324 Swing sort II
Subtotal of C language -- basic data types and their representations
Aging design guide for applets
Object detection - VIDEO reasoning using yolov6
力扣今日题-324. 摆动排序 II
Some high-level usage of localstorage
[c language] [sword finger offer article] - print linked list from end to end
It is the only one in China that Alibaba cloud container service has entered the Forrester leader quadrant
Li Kou daily question - day 30 -594 Longest harmonic subsequence
二叉树的迭代法前序遍历的两种方法
QT (x): control operation
Can I cast int to a variable of type byte? What happens if the value is larger than the range of byte type?