Today we will talk about the associated storage containers in the Qt container class. The so-called associated storage container stores binary groups rather than a single object in the container. Binary groups are generally expressed as <Key-Value>, that is, "Key-Value pairs ". First, let's look at the concept of array. An array can be considered as a Key-value pair in the form of <int-Object>. Its Key can only be int, and the value type is Object, that is, any type. (Note, here we only mean that the array can be of any type, and this Object must not be an Object ). Now we can extend the array concept to make keys of any type, not just int, which is an associated container. If you have learned the data structure, a typical associated container is a Hash Map (Hash table ). Qt provides two types of associated containers: QMap <K, T> and QHash <K, T>. QMap <K, T> is a key-value pair data structure. It is actually implemented using the skip table skip-list and stored in ascending order by K. Use the insert () function of QMap <K, T> to insert data into QMap <K, T>. The typical code is as follows: 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> QMap <QString, int> map;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> map. insert (" eins ", 1 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> map. insert (" sieben ", 7 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif"/> map. insert ("dreiundzwanzig", 23); similarly, QMap <K, T> also loads the [] Operator. You can use the array replication method: 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> map [" eins "] = 1;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> map [" sieben "] = 7;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> Map [" dreiundzwanzig "] = 23; [] the operator can also take the same value as an array. However, note that if you use the [] operator to retrieve a non-const Key value in a non-const map, the Key is automatically created, and assign the associated value to a null value. To avoid this problem, use the value () function of QMap <K, T>: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> Int val = map. value ("dreiundzwanzig"); if the key does not exist, 0 is returned for the basic type and pointer, and the default constructor is called for the object type to return an object. Unlike the [] operator, the value () function does not create a new key-value pair. If you want to return a default value for a key that does not exist, you can pass the second parameter of the value () function: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> Int seconds = map. value (" delay ", 30); this line of code is equivalent to: 650) this. width = 650;" onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> Int seconds = 30;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> if (map. contains (" delay "))
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif"/> seconds = map. value ("delay"); K and T in QMap <K, T> can be basic data types, such as int, double, or pointer, or a class with default constructor, copy constructor, and value assignment operator. In addition, K must be overloaded because QMap <K, T> must be sorted in K ascending order. QMap <K, T> provides the keys () and values () functions to obtain the set of keys and values. Both sets use QList as the return value. Map is a single-value type, that is, if a new value is assigned to an existing key, the old value will be overwritten. If you want a key to index multiple values, you can use QMultiMap <K, T>. This class allows a key to index multiple values, such as: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> QMultiMap <int, QString> multiMap;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> multiMap. insert (1," one ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> multiMap. insert (1," eins ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> multiMap. insert (1," uno ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> QList <QString> vals = multiMap. values (1); QHash <K, T> is a key-value pair that uses Hash Storage. Its interfaces are almost the same as those of QMap <K, T>, but their implementation requirements are different. QHash <K, T> is much faster than QMap <K, T>, and its storage is not sorted. For QHash <K, T>, the type of K must be overloaded with the = Operator and must be supported by the global function qHash (). This function is used to return the hash value of the key. Qt has implemented the qHash () function for int, pointer, QChar, QString, and QByteArray. QHash <K, T> automatically allocates an initial size for the hash, and changes the size of the hash when data is inserted or deleted. We can use the reserve () function to expand the hash, and use the squeeze () function to reduce the hash to the minimum size (the minimum size is actually the minimum space that can store the data ). In use, we can use the reserve () function to expand the data item to the expected maximum value, insert the data, and then use the squeeze () function to shrink the space. QHash <K, T> is also a single value type, but you can use the insertMulti () function or use the QMultiHash <K, T> class to insert multiple values for a key. In addition to QHash <K, T>, Qt also provides QCache <K, T> to provide cache. QSet <K> is used to store only keys. These two classes share the same QHash <K, T> and have K type restrictions. The simplest way to traverse associated storage containers is to use a Java-style traversal tool. Because the next () and previous () Functions of the Java-style traversal tool can return a key-value pair, not just a value, for example, 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> QMap <QString, int> map;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/>...
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> int sum = 0;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> QMapIterator <QString, int> I (map );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> while (I. hasNext ())
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif"/> sum + = I. next (). value (); if we do not need to access a key-value pair, we can directly ignore the return values of the next () and previous () functions, but call the key () and value () functions, for example: 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> QMapIterator <QString, int> I (map );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> while (I. hasNext ()){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> I. next ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> if (I. value ()> largestValue ){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> largestKey = I. key ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> largestValue = I. value ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif"/>} Mutable traversal can modify the value of the key: 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'ign =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> QMutableMapIterator <QString, int> I (map );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> while (I. hasNext ()){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> I. next ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> if (I. value () <0.0)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> I. setValue (-I. value ());
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif"/>} if it is an STL-style traversal, you can use its key () and value () functions. For the foreach loop, We need to cycle the key and value respectively: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> QMultiMap <QString, int> map;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/>...
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> foreach (QString key, map. keys ()){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> foreach (int value, map. values (key )){
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/> doSomething (key, value );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1R12942Z-0.gif "/>}
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/248373