Qt container class
Qt provides a set of generic template-based container classes that are lighter, safer and easier to use than the standard C++ template library. At the same time, speed, memory consumption and inline code has been optimized.
Qt containers must store assignable data types, that is, the type must have a default constructor, copy constructor, and assignment operator.
This data contains most of the commonly used data types, including basic data types and some Qt data types (such as QString). However, Qt’s QObject and other subclasses (qWidgets) cannot be stored in containers. However, Pointers to Qt’s QObject and other subclasses are available.
QList<QToolBar> list; / / error
QList<QToolBar* > list; / / right
Copy the code
Qt’s container classes also support nesting
QHash<QString,QList<double> >;// Note that there must be a space at the end of the >>, otherwise it will be interpreted as >> error
Copy the code
Qt container traversal in two ways: JAVA and STL. STL is recommended (versatile and efficient).
QList, QLinkedList, and QVector classes
Frequently used container classes include QList, QLinkedList, and QVector. In terms of efficiency, the time complexity comparison of these three classes is listed here:
Container classes | Looking for | Inserted into the | Add the head | Add a tail |
---|---|---|---|---|
QList | O(1) | O(n) | Amort.O(1) | Amort.O(1) |
QLinkedList | O(n) | O(1) | O(1) | O(1) |
QVector | O(1) | O(n) | O(n) | O(1) |
Ort.o (1) indicates that O(n) behavior may occur if only one operation is performed, but the average result will be O(1) if multiple operations are performed. |
1. QList class
QList is by far the most commonly used container, and its subclasses are:
-
QItemSelection
-
QQueue
-
QStringList
-
QTestEventList
Qt provides the QList::append() and QList::prepend() functions that can be appended to lists. It also provides two insertable functions, QList::insert(). QList is highly optimized to keep executable code to a minimum.
QList maintains an array of Pointers to the contents of the list items it stores, so it supports fast subscription-based access.
For different data types, QList adopts different storage policies, which can be divided into the following two types:
1.If T is a pointer type or pointer size type, it is stored directly in the container's array.2.If a pointer to an object is stored, the pointer points to the object that is actually stored. For example: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --#include<QDebug>
int main(a){
QList<QString> list; // (a)
{
QString str("This is a test string"); list << str; // (b) } // (c) qDebug() << list[0] << "How are you!" ;return0; } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (a) QList < QString > list: declare a QList < QString > stack objects. (b)list << STR stores a string in a list by the operator. (c) The scope enclosed in curly braces in the program indicates that list is holding a copy of an object.Copy the code
2. The QLinkedList class
QLinkedList is a chained list that holds data in discrete chunks of memory.
QLinkedList cannot use subscripts, only iterators, and is more efficient than QList for inserting large lists.
3. The QVector class
QVector stores a set of values for T in adjacent memory. Inserting into the front or middle of a QVector is slow because of the large amount of memory movement (arrays).
QVector can be accessed either by subscript or iterator. The subclasses of QVector are QPolygon, QPolygonF, and QStack.
4. Java-style iterator traversal
For each container class, Qt provides two java-style iterator data types, read-only access and read-write access, as shown in the table below
Container classes | Only the repeater classes are read | Read/write repeater classes |
---|---|---|
QList,QQueue | QListIteator | QMutableListIterator |
QLinkedList | QLinkedListIteator | QMutableLinkedListIterator |
QVector | QVectorIteator | QMutableVectorIterator |
A Java-style iteration point is in the middle of a list item rather than pointing directly to a list item, so it may be before the first list item, in the middle of two lists, or after the last list item.
QList<int> list;
list << 1 << 2 << 3 << 4 << 5;
QListIterator<int> it(list);
for(; it.hasNext());qDebug() << it.next(a);Copy the code
The result is: 1, 2, 3, 4, 5
The previous example shows backward traversal. The functions that traverse forward include the following
QListIterator::toBack();
Move the iteration point after the last list item
QListIterator::hasPrevious();
Check if there is a list item before the current iteration point
QListIterator::previous();
Returns the contents of the previous list item and moves the iteration point before the previous list item
Other functions provided by QListIterator include the following
toFront(a);// Move the iteration point to the front
peekNext(a);// Returns the next list item, but does not move the iteration point
peekPrevious(a);// Returns the previous list item, but does not move the iteration point
findNext(a);// Look back for a specific list item, true if there is one, false if there is none, iteration point is at the bottom of the list
findPrevious(a);// Similar to findNext, if not, the iteration point is first in the list
Copy the code
QListIterator is a read-only iterator, which cannot complete insert and delete operations. It needs to use QMutableListIterator. Besides the functions of QListIterator, it also has insert() insert function, remove() delete function, etc.
QList<int> list; QMutableListIterator<int> it(list); // write iterator for(int I = 0; i < 10; i++) { it.insert(i); } for(it.toFront(); it.hasNext();) { qDebug() << it.next(); } for(it.toBack(); it.hasPrevious();) { if(it.previous() %2 == 0) it.remove(); else it.setValue(it.peekNext() * 10); } for(it.toFront(); it.hasNext();) { qDebug() << it.next(); }Copy the code
STL style iterator
Container classes | Only the repeater classes are read | Read/write repeater classes |
---|---|---|
QList,QQueue | QList::const_iterator | QList::iterator |
QLinkedList | QLinkedList::const_iterator | QLinkedList::iterator |
QVector | QVector::const_iterator | QVector::iterator |
The API for STL style iterators is based on pointer operations, such as ++ to move to the next item, and * to get the value the iterator points to. | ||
“`cpp |
QList<int> list; for(int j = 0; j < 10; j++) { list.insert(list.end(),j); } QList<int>::iterator i; for(i = list.begin(); i ! = list.end(); ++i) { qDebug() << (*i); *i = (*i) * 10; } QList<int>::const_iterator ci; for(ci=list.constBegin(); ci! =list.constEnd(); ++ci) { qDebug() << *ci; }Copy the code
QMap and QHash classes have very similar functions. They differ in that: - QHash has higher lookup speed than QMap - QHash stores items in any order, while QMap always stores items in the order of keys - Key type of QHash must provide ioeartir==() and a global QHash (Key) function, QMap must provide an operator<() function. Both the complexity of the container classes | key lookup | -- - | | inserted into -- -- -- -- -- - | -- - | -- - | -- - | -- - | | | on average the worst average | the worst QMap | O (log n) | O (log n) | | O (log n) O (log n) QHash | Amort. O (1) (n) | | O Amort. O (1) | O (n) # # # QMap class QMap < T > Key, provides a type from the Key Key to the value of the T type mappings. In general, the data form of QMap is that one Key corresponds to one value. To support multiple values with one Key, the QMap<Key,T>::insertMulti() and QMap<Key,T>::values() functions can be used. QMultiMap<Key,T> containers can also be used to store one-key, multi-value data, which is inherited from QMap. The QHash class is almost identical to QMap's API, maintaining a hash table with a size commensurate with the number of items stored. The QMultiHash<Key,T> container can also be used to store one-key, multi-value data, which is inherited from QHash. QHash is recommended when the order of stored data does not matter. Java iterator style of two kinds of classification of container classes | read-only iterator class | to read and write iterator class -- - | -- - | - QMap < T > Key, QMultiMap<Key,T>| QMapIterator<Key,T> | QMutableMapIterator<Key,T> QMultiHash<Key,T>,QHash<Key,T>|QHashIterator<Key,T> | QMutableHashIterator < T > Key, two kinds of classification of STL iterator style container classes | read-only iterator class | to read and write iterator class -- - | -- - | - QMap < T > Key, QMultiMap<Key,T>|QMap<Key,T>::const_iterator |QMap<Key,T>::iterator QHash QMultiHash < Key, T >, < T > Key, | QHash < Key, T > : : const_iterator | QHash < Key, T > : : iterator ` ` ` CPP here to have a look at the STL calls, JAVA reference List above QMap<QString,QString> map; map.insert("111","aaa"); map.insert("222","bbb"); map.insert("333","ccc"); QMap<QString,QString>::const_iterator cit; for(cit = map.constBegin(); cit ! = map.constEnd(); ++cit) { qDebug() << " " << cit.key() << " " << cit.value(); } QMap<QString,QString>::iterator it; it=map.find("111"); if(it ! = map.end()) { it.value() = "ddd"; } QMap<QString,QString>::const_iterator ccit; for(ccit = map.constBegin(); ccit ! = map.constEnd(); ++ccit) { qDebug() << " " << ccit.key() << " " << ccit.value(); }Copy the code