4. Qt containers and qt containers

Source: Internet
Author: User

4. Qt containers and qt containers

Qt provides a set of common template-based container classes.

I. QList class, qinilist class and QVector class

The Qt container classes frequently used by the QList, qinilist, and QVector classes include QList, qinilist, and QVector. When developing an application with high performance requirements, programmers will pay more attention to the running efficiency of these container classes. The following table lists the time complexity of QList, q1_list, and QVector containers.

"Amort. O (1)" indicates that only one operation is completed. There may be an O (n) action.

(1) QList class

QList <T> is the most common container class so far. It stores a column of values of the given data type T. The child classes inherited from the QList class: QItemSelection, QQueue, QSignalSpy, QStringList, and QTestEventList.
QList provides the QList: append () and Qlist: prepend () functions that can be appended to the list. It also provides the function QList :: insert (). Compared to any other Qt container class, QList is highly optimized to minimize the number of executable code.
QList <T> maintains a pointer array. the pointer stored in this array points to the content of the list item stored in QList <T>.


#include <QDebug>#include <QList>int main(){    QList<QString> list;    {        QString str("This is a test string");        list<<str;    }    qDebug()<<list[0]<<"  good evening";    return 0;}



* QList <QString> list: declares a QList <QString> stack object.

* List <str; stores the QString in the list using the operator "<.


(2) qinilist class

Qinilist <T> is a chained list that stores data in non-contiguous memory blocks.
Qinilist <T> cannot use subscript, but can only use iterator to access its data items. Compared with QList, q1_list is more efficient when inserting a large list.

(3) QVector class

QVector <T> stores a group of values of the given data type T in the adjacent memory. Inserting data at the front or middle of a QVector is slow, which is determined by the way QVector stores data.


STL-style iterative container class traversal container

For each container class, Qt provides two types of STL-style iterator data types: read-only access and read/write access. Since the read-only type iterator is faster than the read/write iterator, you should try to use the read-only type iterator as much as possible. The two types of style iterators are represented by tables.



<Pre name = "code" class = "cpp"> int main () {QList <int> list; for (int j = 0; j <10; j ++) list. insert (list. end (), j); QList <int>: iterator I; // initialize a read/write iterator with the pointer type for (I = list. begin (); I! = List. end (); ++ I) {qDebug () <(* I); * I = (* I) * 10;} QList <int>: const_iterator ci; // initialize a read-only iterator for (ci = list. constBegin (); ci! = List. constEnd (); ++ ci) qDebug () <* ci; return 0 ;}


 


Ii. QMap and QHash

The QMap class and the QHash class have very similar functions. Their differences are as follows:
QHash has a faster search speed than QMap.
QHash stores data items in any order, while QMap stores data in Key-Key order.
The Key type Key of QHash must provide operator = () and a global qHash (Key) function, while the Key of QMap must provide operator <() function.

1. QMap class

QMap <Key, T> provides a ing from a Key of type to a value of type T.
QMap stores data in the form of a Key corresponding to a value, and stores data in the order of Key keys. To support multiple values with one click, QMap provides the QMap <Key, T>: insertMulti () and QMap <Key, T>: values () functions. You can also use the QMultiMap <Key, T> container to store one-click multi-value data, which inherits from QMap.
2. QHash class
QHash <Key, T> has an API that is almost identical to QMap. QHash maintains a hash table, which is suitable for the size of the hash table and the number of QHash data items.

3. STL-style iterator container Traversal

For each container class, Qt provides two types of STL-style iterator data types: read-only access and read/write access.


Int main () {QMap <QString, QString> map; map. insert ("bj", "1111"); map. insert ("qhd", "222"); map. insert ("corner stone", "3333"); QMap <QString, QString >:: Iterator mi; // read/write Iterator mi = map. find ("bj"); if (mi! = Map. end () mi. value () = "010"; QMap <QString, QString>: const_iterator modi; // read-only iterator qDebug () <""; for (modi = map. constBegin (); modi! = Map. constEnd (); ++ modi) qDebug () <"" <modi. key () <"" <modi. value (); return 0 ;}

Iii. QVariant class

The QVariant class is similar to the union data type of C ++. It can save many Qt values, including QColor, QBrush, QFont, QPen, QRect, QString, and QSize, it can also store the value of the Qt container type. Many functions of Qt are based on QVariant, such as object attributes of Qt and database functions.

# Include <QDebug> # include <QVariant> # include <QColor> int main () {QVariant v (709); // declare a QVariant variable v, which is initialized as an integer. qDebug () <v. toInt (); // convert to an integer and output // v. QVariant ("How are you! "); // This compilation method does not pass. v = QVariant (" How are you! "); // Change the value of v to the string qDebug () <v. toString (); QMap <QString, QVariant> map; // declare the QMap variable map ["int"] = 709; map ["double"] = 709.709; map ["string"] = "How are you! "; Map [" color "] = QColor (255, 0, 0); qDebug () <map [" int "] <map [" int "]. toInt (); qDebug () <map ["double"] <map ["double"]. toDouble (); qDebug () <map ["string"] <map ["string"]. toString (); qDebug () <map ["color"] <map ["color"]. value <QColor> (); QStringList s1; // create A string list s1 <"A" <"B" <"c" <"D "; QVariant slv (s1); // Save the list in the QVariant variable if (slv. type () = QVariant: StringList) {QStringList = slv. toStringList (); for (int I = 0; I <list. size (); ++ I) qDebug () <list. at (I) ;}return 0 ;}

Output result:



Iv. Qt Algorithm

1. Qt's <QtAlgorithms> and <QtGlobal> modules provide some algorithms and functions.

Int main () {double a =-19.3, B = 9.7; double c = qAbs (a); // returns the absolute value of double max = qMax (B, c ); // returns the maximum value int bn = qRound (B); // returns an integer int cn = qRound (c); // qDebug () <"a =" <a; qDebug () <"B =" <B; qDebug () <"c = qAbs () = "<c; qDebug () <" qMax (B, c) = "<max; qDebug () <" bn = qRound (B) = "<bn; qDebug () <" cn = qRound (c) = "<cn; qSwap (bn, cn ); // exchange the values of qDebug () <"qSwap (bn, cn):" <"bn =" <bn <"cn =" <cn; return 0 ;}

Output:

A =-19.3

B = 9.7

C = qAbs (a) = 1, 19.3

QMax (B, c) = 19.3

Bn = qRound (B) = 10

Cn = qRound (c) = 19

QSwap (bn, cn): bn = 19 cn = 10


2. Basic Regular Expression

A regular expression is composed of expressions (expressions), quantifiers (quantifiers), and assertions.
(1) The simplest expression is a character. The expression used to represent the character set can use an expression similar to "[AEIOU]" to indicate that all uppercase vowels are matched. If "[^ AEIOU]" is used, all non-vowels are matched, that is, consonants. Continuous character sets can use expressions such as [a-z] to match all lowercase English letters.
(2) quantifiers indicate the number of times the expression appears. For example, "x [1, 2]" indicates that "x" can have at least one and at most two.
In computer languages, identifiers generally start with letters or underlines, followed by letters, numbers, and underlines. The identifier that meets the condition is:
"[A-Za-z _] + [A-Za-z_0-9] *"

(3) "^", "$", and "\ B" are assertions of regular expressions.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.