QT provides a common set of template-based container classes.
I. Qlist class, Qlinkedlist class and Qvector class
the QT container classes commonly used by qlist, Qlinkedlist, and Qvector classes are qlist, Qlinkedlist, and Qvector. When developing an application with a higher performance requirement. Program apes will pay more attention to the efficiency of these container classes. The following table lists the time complexity of the qlist, qlinkedlist, and Qvector containers.
In: "AMORT.O (1)" means that only one operation is completed. There may be an O (n) behavior.
(1) Qlist class
qlist<t> is the most commonly used container class so far. It stores a column of values for a given data type T. Inherits from subclasses of the Qlist class Qitemselection, Qqueue, Qsignalspy, and Qstringlist and Qtesteventlist.
Qlist provides qlist::append () and qlist: the:p repend () function that can be appended to the list. A function Qlist::insert () that completes the insertion operation in the middle of the list is also provided. Compared to whatever other QT container classes are. In order to make the running code as small as possible. The qlist is highly optimized.
Qlist<t> maintains a pointer array that stores pointers to the contents of qlist<t> stored list items.
#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; The qstring string is stored in the list by manipulating the operator "<<".
(2) Qlinkedlist class
Qlinkedlist<t> is a chained list that holds data in a non-contiguous block of memory.
Qlinkedlist<t> the subscript cannot be used. You can simply use an iterator to access its data items. When inserting to a very large list, compared to qlist. The qlinkedlist has higher efficiency.
(3) Qvector class
Qvector<t> stores a set of values for a given data type T in adjacent memory.
It is very slow to insert operations in the front or middle of a qvector, which is determined by how Qvector stores the data.
STL Style Iterative container class traversal container
For each container class, QT provides two types of STL-style iterator data types: one that provides read-only access, and one that provides read-write access. Because a read-only type of iterator is faster than a read-write iterator, you should use a read-only type iterator whenever possible. Two style iterators are categorized as table representations.
<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;//Initializes a read-write iterator to the pointer type for (I=list.begin (); I!=list.end (); ++i) { qdebug () << (*i); *i = (*i) *10; } Qlist<int>::const_iterator ci;//Initializes a read-only iterator for (CI = list.constbegin (); CI! = List.constend (); ++ci) Qdebug () <<*ci; return 0;}
Two. Qmap class and Qhash class
The Qmap class and the Qhash class have very similar functions. They differ only in the following:
the Qhash has a faster lookup rate than the Qmap.
Qhash stores data items in a random order. Qmap always stores data in the order of key keys.
The Qhash key type key must provide a operator== () and a global Qhash (key) function, and the key type key of the Qmap must provide the operator< () function.
1. Qmap class
Qmap<key,t> provides a mapping from a key of type key to a value of type T.
The data form stored by the Qmap is a key corresponding to a value, and the data is stored in the order of key keys. Qmap provides the Qmap<key,t>::insertmulti () and qmap<key,t>::values () functions in order to support a single-key multi-value scenario. You can also use the Qmultimap<key,t> container when storing one-key multivalued data, which inherits from Qmap.
2. Qhash class
Qhash<key,t> has nearly the same API as Qmap.
Qhash maintains a hash table (hash table). The size of the hash table fits with 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: one that provides read-only access. One that provides read and write access.
int main () { qmap<qstring,qstring> map; Map.insert ("BJ", "1111"); Map.insert ("qHD", "222"); Map.insert ("TJ", "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;//only Read iterators qdebug () << " "; For (Modi=map.constbegin (); Modi! = Map.constend (); ++modi) qdebug () << "<<modi.key () <<" "< <modi.value (); return 0;}
Three. Qvariant class
the Qvariant class is similar to the Union (Union) data type of C + +. It can hold values of very many QT types. Includes Qcolor, Qbrush, Qfont, Qpen, Qrect, QString, qsize, etc. The value of the container type of QT can also be stored.
Qt's very versatility is built on the basis of qvariant, such as the object properties of QT and database functions.
#include <QDebug> #include <QVariant> #include <qcolor>int main () {Qvariant V (709);//Declare a qvariant variable V , initialized to an integer. Qdebug () <<v.toint (); Convert to integers, output//v.qvariant ("How is you!"); Such a compilation does not pass. v = qvariant ("How is you!"); /change the value of V to string Qdebug () <<v.tostring (); Qmap<qstring,qvariant> map; DECLARE qmap variable map map["int"] = 709; map["Double"] = 709.709; Map["string"] = "how is 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 list=slv.tostringlist (); for (int i=0;i<lIst.size (); ++i) Qdebug () <<list.at (i); } return 0;}
Output Result:
Four. QT algorithm
1. QT's <QtAlgorithms> and <QtGlobal> modules provide a number of 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); rounding returns an integer int cn = Qround (c); qdebug () << "a=" <<a; Qdebug () << "b=" <<b; Qdebug () << "C=qabs (a) =" <<c; Qdebug () << "QMax (b,c) =" <<max; Qdebug () << "Bn=qround (b) =" <<bn; Qdebug () << "Cn=qround (c) =" <<cn; Qswap (BN,CN); Exchange two number of values qdebug () << "Qswap (BN,CN):" << "bn=" <<bn<< "cn=" <<cn; return 0;}
Output:
A=-19.3
b= 9.7
C=qabs (a) = 19.3
QMax (b,c) = 19.3
Bn=qround (b) = 10
Cn=qround (c) = 19
Qswap (BN,CN): bn= cn= 10
2. Basic regular Form
The regular form consists of an expression (expressions), a quantifier (quantifiers), and an assertion (assertions).
(1) The simplest expression is a character. Expressions that are to represent a character set can use similar words as "[Aeiou]" to match all uppercase vowels, or "[^aeiou]" to match all non-vowels, i.e. consonant letters, and consecutive character sets that use expressions such as "[A-z]" to match all lowercase English letters.
(2) quantifiers indicate the number of occurrences of an expression, such as "x[1,2", which means that "X" can have at least one. Maximum of two.
In the computer language. Identifiers usually require the beginning of a letter or line, followed by a letter, a number, and a drawing line. Identifiers that satisfy a condition are represented as:
"[a-za-z_]+[a-za-z_0-9]*]
⑶ "^", "$", "\b" are all predicate assertions.
4. QT's Container class