Qt容器類彙總說明

版權聲明:若無來源註明, Techie亮博客文章均爲原創。 轉載請以連接形式標明本文標題和地址:
本文標題:Qt容器類彙總說明     本文地址: http://techieliang.com/2017/12/542/

下述說明來源於官方文檔html

1. 介紹

Qt庫提供了一組通用的基於模板的容器類。這些類可用於存儲指定類型的項。例如,若是你須要一個可調整大小的數組qstrings,使用QVector <QString>。c++

這些容器類的設計要比STL容器更輕、更安全、更容易使用。若是您不熟悉STL,或者更喜歡作「qt方式」,您能夠使用這些類而不是STL類。算法

Qt還提供了一個foreach關鍵字,讓它來遍歷全部的物品存放在一個容器很容易。api

qt提供的foreach在c++標準中並無,在c++11中提供了相似於for(auto t:container)方式遍歷容器,此方式qt也支持,我的感受使用for更好數組

……更多介紹看官網安全

2. 本博客的Qt容器使用說明

QMapQMultiMap、(當前內容截止此隨筆發佈日,後續內容請見博客後續更新   本文地址:http://techieliang.com/2017/12/542/)app

3. 容器類

Qt提供了下列順序容器:QList,QLinkedList,QVector,QStack,和QQueue。對於大多數應用程序,QList是使用最好的類型。雖然它底層用數組鏈表實現,但提供了很是快速的先後追加函數。若是你真的須要一個鏈表,用QLinkedList;若是你想讓你的項目佔用連續的內存位置,使用QVector。QStack 和QQueue提供了先進先出(LIFO ),先結後出(FIFO )。ide

Qt也提供了關聯式容器:?QMap,?QMultiMap,?QHash,?QMultiHash,和QSet.?。Multi容器支持多個value關聯到單一的key。Hash容器提供了經過hash函數快速查找取代二分查找。函數

特殊狀況下,?QCache?和?QContiguousCache?提供了在有限的cache?中高效的查找對象。ui

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)
相關文章
相關標籤/搜索