Qt容器类汇总说明
本文标题:Qt容器类汇总说明 本文地址:http://techieliang.com/2017/12/542/
下述说明来源于官方文档
1. 介绍
Qt库提供了一组通用的基于模板的容器类。这些类可用于存储指定类型的项。例如,如果你需要一个可调整大小的数组qstrings,使用QVector <QString>。
这些容器类的设计要比STL容器更轻、更安全、更容易使用。如果您不熟悉STL,或者更喜欢做“qt方式”,您可以使用这些类而不是STL类。
Qt还提供了一个foreach关键字,让它来遍历所有的物品存放在一个容器很容易。
qt提供的foreach在c++标准中并没有,在c++11中提供了类似于for(auto t:container)方式遍历容器,此方式qt也支持,个人感觉使用for更好
……更多介绍看官网
2. 本博客的Qt容器使用说明
QMap、QMultiMap、(当前内容截止此随笔发布日,后续内容请见博客后续更新 本文地址:http://techieliang.com/2017/12/542/)
3. 容器类
Qt提供了下列顺序容器:QList,QLinkedList,QVector,QStack,和QQueue。对于大多数应用程序,QList是使用最好的类型。虽然它底层用数组链表实现,但提供了非常快速的前后追加函数。如果你真的需要一个链表,用QLinkedList;如果你想让你的项目占用连续的内存位置,使用QVector。QStack 和QQueue提供了先进先出(LIFO ),先结后出(FIFO )。
Qt也提供了关联式容器:?QMap,?QMultiMap,?QHash,?QMultiHash,和QSet.?。Multi容器支持多个value关联到单一的key。Hash容器提供了通过hash函数快速查找取代二分查找。
特殊情况下,?QCache?和?QContiguousCache?提供了在有限的cache?中高效的查找对象。
| Class | Summary |
|---|---|
| QList<T> | This is by far the most commonly used container class. It stores a list of values of a given type (T) that can be accessed by index. Internally, the QList is implemented using an array, ensuring that index-based access is very fast.Items can be added at either end of the list using QList::append() and QList::prepend(), or they can be inserted in the middle using QList::insert(). More than any other container class, QList is highly optimized to expand to as little code as possible in the executable. QStringList inherits from QList<QString>. |
| QLinkedList<T> | This is similar to QList, except that it uses iterators rather than integer indexes to access items. It also provides better performance than QList when inserting in the middle of a huge list, and it has nicer iterator semantics. (Iterators pointing to an item in a QLinkedList remain valid as long as the item exists, whereas iterators to a QList can become invalid after any insertion or removal.) |
| QVector<T> | This stores an array of values of a given type at adjacent positions in memory. Inserting at the front or in the middle of a vector can be quite slow, because it can lead to large numbers of items having to be moved by one position in memory. |
| QStack<T> | This is a convenience subclass of QVector that provides “last in, first out” (LIFO) semantics. It adds the following functions to those already present in QVector: push(), pop(), and top(). |
| QQueue<T> | This is a convenience subclass of QList that provides “first in, first out” (FIFO) semantics. It adds the following functions to those already present in QList: enqueue(), dequeue(), and head(). |
| QSet<T> | This provides a single-valued mathematical set with fast lookups. |
| QMap<Key, T> | This provides a dictionary (associative array) that maps keys of type Key to values of type T. Normally each key is associated with a single value. QMap stores its data in Key order; if order doesn’t matter QHash is a faster alternative. |
| QMultiMap<Key, T> | This is a convenience subclass of QMap that provides a nice interface for multi-valued maps, i.e. maps where one key can be associated with multiple values. |
| QHash<Key, T> | This has almost the same API as QMap, but provides significantly faster lookups. QHash stores its data in an arbitrary order. |
| QMultiHash<Key, T> | This is a convenience subclass of QHash that provides a nice interface for multi-valued hashes. |
4. 迭代器类
Qt提供了多种风格的迭代器,Java风格和stl风格
4.1. Java风格迭代器
| Containers | Read-only iterator | Read-write iterator |
|---|---|---|
| QList<T>, QQueue<T> | QListIterator<T> | QMutableListIterator<T> |
| QLinkedList<T> | QLinkedListIterator<T> | QMutableLinkedListIterator<T> |
| QVector<T>, QStack<T> | QVectorIterator<T> | QMutableVectorIterator<T> |
| QSet<T> | QSetIterator<T> | QMutableSetIterator<T> |
| QMap<Key, T>, QMultiMap<Key, T> | QMapIterator<Key, T> | QMutableMapIterator<Key, T> |
| QHash<Key, T>, QMultiHash<Key, T> | QHashIterator<Key, T> | QMutableHashIterator<Key, T> |
4.2. STL风格迭代器
| Containers | Read-only iterator | Read-write iterator |
|---|---|---|
| QList<T>, QQueue<T> | QList<T>::const_iterator | QList<T>::iterator |
| QLinkedList<T> | QLinkedList<T>::const_iterator | QLinkedList<T>::iterator |
| QVector<T>, QStack<T> | QVector<T>::const_iterator | QVector<T>::iterator |
| QSet<T> | QSet<T>::const_iterator | QSet<T>::iterator |
| QMap<Key, T>, QMultiMap<Key, T> | QMap<Key, T>::const_iterator | QMap<Key, T>::iterator |
| QHash<Key, T>, QMultiHash<Key, T> | QHash<Key, T>::const_iterator | QHash<Key, T>::iterator |
5. Qt提供的其他容器
Qt includes three template classes that resemble containers in some
respects. These classes don’t provide iterators and cannot be used with
the foreach keyword.
- QVarLengthArray<T, Prealloc> provides a low-level variable-length array. It can be used instead of QVector in places where speed is particularly important.
- QCache<Key, T> provides a cache to store objects of a certain type T associated with keys of type Key.
- QContiguousCache<T> provides an efficient way of caching data that is typically accessed in a contiguous way.
- QPair<T1, T2> stores a pair of elements.
Additional non-template types that compete with Qt’s template containers are QBitArray, QByteArray, QString, and QStringList.
6. Qt容器算法复杂性
Algorithmic complexity is concerned about how fast (or slow) each
function is as the number of items in the container grow. For example,
inserting an item in the middle of a QLinkedList is an extremely fast operation, irrespective of the number of items stored in the QLinkedList. On the other hand, inserting an item in the middle of a QVector is potentially very expensive if the QVector contains many items, since half of the items must be moved one position in memory.
To describe algorithmic complexity, we use the following terminology, based on the “big Oh” notation:
- Constant time: O(1). A function is said to run in constant
time if it requires the same amount of time no matter how many items are
present in the container. One example is QLinkedList::insert(). - Logarithmic time: O(log n). A function that runs in
logarithmic time is a function whose running time is proportional to the
logarithm of the number of items in the container. One example is
qBinaryFind(). - Linear time: O(n). A function that runs in linear time
will execute in a time directly proportional to the number of items
stored in the container. One example is QVector::insert(). - Linear-logarithmic time: O(n log n). A function
that runs in linear-logarithmic time is asymptotically slower than a
linear-time function, but faster than a quadratic-time function. - Quadratic time: O(n?). A quadratic-time function
executes in a time that is proportional to the square of the number of
items stored in the container.
The following table summarizes the algorithmic complexity of Qt’s sequential container classes:
| Index lookup | Insertion | Prepending | Appending | |
|---|---|---|---|---|
| QLinkedList<T> | O(n) | O(1) | O(1) | O(1) |
| QList<T> | O(1) | O(n) | Amort. O(1) | Amort. O(1) |
| QVector<T> | O(1) | O(n) | O(n) | Amort. O(1) |
In the table, “Amort.” stands for “amortized behavior”. For example,
“Amort. O(1)” means that if you call the function only once, you might
get O(n) behavior, but if you call it multiple times (e.g., n times), the average behavior will be O(1).
The following table summarizes the algorithmic complexity of Qt’s associative containers and sets:
| Key lookup | Insertion | |||
|---|---|---|---|---|
| Average | Worst case | Average | Worst case | |
| QMap<Key, T> | O(log n) | O(log n) | O(log n) | O(log n) |
| QMultiMap<Key, T> | O(log n) | O(log n) | O(log n) | O(log n) |
| QHash<Key, T> | Amort. O(1) | O(n) | Amort. O(1) | O(n) |
| QSet<Key> | Amort. O(1) | O(n) | Amort. O(1) | O(n) |
Qt容器类汇总说明的更多相关文章
- Qt——容器类(译)
注:本文是我对Qt官方文档的翻译,错误之处还请指正. 原文链接:Container Classes 介绍 Qt库提供了一套通用的基于模板的容器类,可以用这些类存储指定类型的项.比如,你需要一个大小可变 ...
- Qt容器类之二:迭代器
一.介绍 遍历一个容器可以使用迭代器(iterators)来完成,迭代器提供了一个统一的方法来访问容器中的项目.Qt的容器类提供了两种类型的迭代器:Java风格迭代器和STL风格迭代器.如果只是想按顺 ...
- Qt容器类之一:Qt的容器类介绍
一.介绍 Qt库提供了一套通用的基于模板的容器类,可以用这些类存储指定类型的项.比如,你需要一个大小可变的QString的数组,则使用QVector<QString>. 这些容器类比STL ...
- Qt容器类——1. QList类、QLinkedList类和QVector类
在开发一个较高性能需求的应用程序时,程序员会比较关注这些容器类的运行效率,表2.1列出了QList.QLinkedList和QVector容器的时间复杂度比较. 1.QList类 QList<T ...
- Qt容器类的对象模型及应用(线性结构篇)(好多图,比较清楚)
用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类 ...
- Qt容器类(总结)(新发现的QQueue和QStack,注意全都是泛型)
Introduction Qt库提供了一组基于模板的一般化的容器类.这些容器可以存储指定的类型的元素.例如,如果你需要一个可变大小的Qstring数组,可以用QVector<QString> ...
- Qt容器类之三:通用算法
在<QtAlgorithm>头文件中,Qt提供了一些全局的模板函数,这些函数是可以使用在容器上的十分常用的算法.我们可以在任何提供了STL风格迭代器的容器类上用这些算法,包括QList.Q ...
- Qt容器类的对象模型及应用(线性结构篇:对于QList来说,sharable默认是false的,但对于接下来讲的QVector来说,sharable默认是true)
用Qt做过项目开发的人,肯定使用过诸如QList.QVector.QLinkList这样的模板容器类,它们虽然名字长的不同,但使用方法都大致相同, 因为其使用方法都大体相同,很多人可能随便拿一个容器类 ...
- Qt Examples Qt实例汇总
ActiveQt Examples Using ActiveX from Qt applications. Animation Framework Examples Doing animations ...
随机推荐
- python3 str各个功能记录
capitalize() 将字符串的第一个字符转换为大写 center(width, fillchar) 返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格. c ...
- Python enumerate()方法
for循环中如果要获取当前元素的索引值,一个方法是定义一个计数器,每次取值的时候将这个值加一,如果是列表的话可以用index()函数,而python中有一个比较简洁的方法而已直接获得索引值,并可以方便 ...
- 20155319《Java程序设计》实验三(敏捷开发与XP实践)实验报告
20155319<Java程序设计>实验三(敏捷开发与XP实践)实验报告 一.实验内容及步骤 (一)使用Code菜单 在IDEA中使用工具(Code->Reformate Code) ...
- OpenStack入门篇(一)之云计算的概念
1.云计算 云计算是一种按使用量付费的模式,这种模式提供可用的.便捷的.按需的网络访问, 进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用软件,服务),这些资源能够被快速提供,只需投入很少 ...
- 深入解析QML引擎, 第1部分:QML文件加载
译者注:这个解析QML引擎的文章共4篇,分析非常透彻,在国内几乎没有找到类似的分析,为了便于国内的QT/QML爱好者和工作者也能更好的学习和理解QML引擎,故将这个系列的4篇文章翻译过来.翻译并不是完 ...
- Python如何判断变量的类型
Python判断变量的类型有两种方法:type() 和 isinstance() 如何使用 对于基本的数据类型两个的效果都一样 type() ip_port = ['219.135.164.245', ...
- 622.设计循环队列 javascript实现
设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为“环形缓冲器”. 循环队列的一个好处是我们可以利用这个队列 ...
- kubeadm构建k8s之Prometheus-operated监控(0.18.1)
介绍: 大家好,k8s的搭建有许多方式,也有许多快速部署的,为了简化部署的复杂度,官方也提供了开源的kubeadm快速部署,最新1.10.x版本已经可以实现部署集群, 如果你对k8s的原理已经非常了解 ...
- 译 - Cassandra 数据建模的基本规则
Basic Rules of Cassandra Data Modeling 原文地址:http://www.datastax.com/dev/blog/basic-rules-of-cassandr ...
- [python]序列的重复操作符
当你需要需要一个序列的多份拷贝时,重复操作符非常有用,它的语法如下: sequence * copies_int In [1]: a = [1,2,3,4] In [2]: a * 5 Out[2]: ...