The QHash 類是提供基於哈希錶的字典的模闆類。 更多...
| 頭: | #include <QHash> |
| 繼承者: | QMultiHash |
注意: 此類的所有函數 可重入 .
| class | const_iterator |
| class | iterator |
| typedef | ConstIterator |
| typedef | Iterator |
| typedef | difference_type |
| typedef | key_type |
| typedef | mapped_type |
| typedef | size_type |
| QHash () | |
| QHash (const QHash<Key, T> & other ) | |
| ~QHash () | |
| iterator | begin () |
| const_iterator | begin () const |
| int | capacity () const |
| void | clear () |
| const_iterator | constBegin () const |
| const_iterator | constEnd () const |
| const_iterator | constFind (const Key & key ) const |
| bool | contains (const Key & key ) const |
| int | count (const Key & key ) const |
| int | count () const |
| bool | empty () const |
| iterator | end () |
| const_iterator | end () const |
| iterator | erase (iterator pos ) |
| iterator | find (const Key & key ) |
| const_iterator | find (const Key & key ) const |
| iterator | insert (const Key & key , const T & value ) |
| iterator | insertMulti (const Key & key , const T & value ) |
| bool | isEmpty () const |
| const Key | key (const T & value ) const |
| const Key | key (const T & value , const Key & defaultKey ) const |
| QList<Key> | keys () const |
| QList<Key> | keys (const T & value ) const |
| int | remove (const Key & key ) |
| void | reserve (int size ) |
| int | size () const |
| void | squeeze () |
| void | swap (QHash<Key, T> & other ) |
| T | take (const Key & key ) |
| QList<Key> | uniqueKeys () const |
| QHash<Key, T> & | unite (const QHash<Key, T> & other ) |
| const T | value (const Key & key ) const |
| const T | value (const Key & key , const T & defaultValue ) const |
| QList<T> | values () const |
| QList<T> | values (const Key & key ) const |
| bool | operator!= (const QHash<Key, T> & other ) const |
| QHash<Key, T> & | operator= (const QHash<Key, T> & other ) |
| QHash<Key, T> & | operator= (QHash<Key, T> && other ) |
| bool | operator== (const QHash<Key, T> & other ) const |
| T & | operator[] (const Key & key ) |
| const T | operator[] (const Key & key ) const |
| uint | qHash (const QXmlNodeModelIndex & index ) |
| uint | qHash (char key ) |
| uint | qHash (uchar key ) |
| uint | qHash (signed char key ) |
| uint | qHash (ushort key ) |
| uint | qHash (short key ) |
| uint | qHash (uint key ) |
| uint | qHash (int key ) |
| uint | qHash (ulong key ) |
| uint | qHash (long key ) |
| uint | qHash (quint64 key ) |
| uint | qHash (qint64 key ) |
| uint | qHash (QChar key ) |
| uint | qHash (const QByteArray & key ) |
| uint | qHash (const QString & key ) |
| uint | qHash (const QBitArray & key ) |
| uint | qHash (const T * key ) |
| uint | qHash (const QPair<T1, T2> & key ) |
| QDataStream & | operator<< (QDataStream & out , const QHash<Key, T> & hash ) |
| QDataStream & | operator>> (QDataStream & in , QHash<Key, T> & hash ) |
The QHash 類是提供基於哈希錶的字典的模闆類。
QHash <Key, T> 是一種 Qt 一般 容器類 。它存儲 (鍵,值) 對,並提供鍵關聯值的非常快速查找。
這裏是範例
QHash
with
QString
鍵和
int
值:
QHash<QString, int> hash;
要將 (鍵,值) 對插入哈希,可以使用 operator[]():
hash["one"] = 1; hash["three"] = 3; hash["seven"] = 7;
This inserts the following three (key, value) pairs into the QHash : ("one", 1), ("three", 3), and ("seven", 7). Another way to insert items into the hash is to use insert ():
hash.insert("twelve", 12);
要查找值,使用 operator[]() 或 value ():
int num1 = hash["thirteen"]; int num2 = hash.value("thirteen");
If there is no item with the specified key in the hash, these functions return a default-constructed value.
If you want to check whether the hash contains a particular key, use contains ():
int timeout = 30; if (hash.contains("TIMEOUT")) timeout = hash.value("TIMEOUT");
There is also a value () overload that uses its second argument as a default value if there is no item with the specified key:
int timeout = hash.value("TIMEOUT", 30);
In general, we recommend that you use contains () 和 value () rather than operator[]() for looking up a key in a hash. The reason is that operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code snippet will create 1000 items in memory:
// WRONG QHash<int, QWidget *> hash; ... for (int i = 0; i < 1000; ++i) { if (hash[i] == okButton) cout << "Found button at index " << i << endl; }
要避免此問題,替換
hash[i]
with
hash.value(i)
在以上代碼中。
If you want to navigate through all the (key, value) pairs stored in a QHash , you can use an iterator. QHash provides both Java 風格迭代器 ( QHashIterator and QMutableHashIterator ) 和 STL 樣式迭代器 ( QHash::const_iterator and QHash::iterator ). Here's how to iterate over a QHash < QString , int> using a Java-style iterator:
QHashIterator<QString, int> i(hash); while (i.hasNext()) { i.next(); cout << i.key() << ": " << i.value() << endl; }
Here's the same code, but using an STL-style iterator:
QHash<QString, int>::const_iterator i = hash.constBegin(); while (i != hash.constEnd()) { cout << i.key() << ": " << i.value() << endl; ++i; }
QHash is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap .
Normally, a QHash allows only one value per key. If you call insert () with a key that already exists in the QHash , the previous value is erased. For example:
hash.insert("plenty", 100); hash.insert("plenty", 2000); // hash.value("plenty") == 2000
However, you can store multiple values per key by using insertMulti () 而不是 insert () (or using the convenience subclass QMultiHash ). If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList <T>:
QList<int> values = hash.values("plenty"); for (int i = 0; i < values.size(); ++i) cout << values.at(i) << endl;
The items that share the same key are available from most recently to least recently inserted. A more efficient approach is to call find () to get the iterator for the first item with a key and iterate from there:
QHash<QString, int>::iterator i = hash.find("plenty"); while (i != hash.end() && i.key() == "plenty") { cout << i.value() << endl; ++i; }
If you only need to extract the values from a hash (not the keys), you can also use foreach :
QHash<QString, int> hash; ... foreach (int value, hash) cout << value << endl;
Items can be removed from the hash in several ways. One way is to call remove (); this will remove any item with the given key. Another way is to use QMutableHashIterator::remove (). In addition, you can clear the entire hash using clear ().
QHash 's key and value data types must be 可賦值數據類型 。例如,無法存儲 QWidget 作為值;取而代之,存儲 QWidget *. In addition, QHash 's key type must provide operator==(), and there must also be a global qHash () function that returns a hash value for an argument of the key's type.
Here's a list of the C++ and Qt types that can serve as keys in a
QHash
: any integer type (char, unsigned long, etc.), any pointer type,
QChar
,
QString
,和
QByteArray
. For all of these, the
<QHash>
header defines a
qHash
() function that computes an adequate hash value. If you want to use other types as the key, make sure that you provide operator==() and a
qHash
() 實現。
範例:
#ifndef EMPLOYEE_H #define EMPLOYEE_H class Employee { public: Employee() {} Employee(const QString &name, const QDate &dateOfBirth); ... private: QString myName; QDate myDateOfBirth; }; inline bool operator==(const Employee &e1, const Employee &e2) { return e1.name() == e2.name() && e1.dateOfBirth() == e2.dateOfBirth(); } inline uint qHash(const Employee &key) { return qHash(key.name()) ^ key.dateOfBirth().day(); } #endif // EMPLOYEE_H
The
qHash
() function computes a numeric value based on a key. It can use any algorithm imaginable, as long as it always returns the same value if given the same argument. In other words, if
e1 == e2
,那麼
qHash(e1) == qHash(e2)
must hold as well. However, to obtain good performance, the
qHash
() function should attempt to return different hash values for different keys to the largest extent possible.
In the example above, we've relied on Qt's global qHash (const QString &) to give us a hash value for the employee's name, and XOR'ed this with the day they were born to help produce unique hashes for people with the same name.
在內部,
QHash
uses a hash table to perform lookups. Unlike Qt 3's
QDict
class, which needed to be initialized with a prime number,
QHash
's hash table automatically grows and shrinks to provide fast lookups without wasting too much memory. You can still control the size of the hash table by calling
reserve
() if you already know approximately how many items the
QHash
will contain, but this isn't necessary to obtain good performance. You can also call
capacity
() to retrieve the hash table's size.
另請參閱 QHashIterator , QMutableHashIterator , QMap ,和 QSet .
Qt 樣式同義詞 QHash::const_iterator .
Qt 樣式同義詞 QHash::iterator .
typedef 對於 ptrdiff_t。為兼容 STL 提供。
typedef 對於 Key。為兼容 STL 提供。
typedef 對於 T。為兼容 STL 提供。
typedef 對於 int。為兼容 STL 提供。
構造空哈希。
另請參閱 clear ().
構造副本為 other .
This operation occurs in 常量時間 ,因為 QHash is 隱式共享 . This makes returning a QHash from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takes 綫性時間 .
另請參閱 operator= ().
Destroys the hash. References to the values in the hash and all iterators of this hash become invalid.
Returns an STL-style iterator pointing to the first item in the hash.
另請參閱 constBegin () 和 end ().
這是重載函數。
Returns the number of buckets in the QHash 的內部哈希錶。
The sole purpose of this function is to provide a means of fine tuning QHash 's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the hash, call size ().
從哈希移除所有項。
另請參閱 remove ().
Returns a const STL-style iterator pointing to the first item in the hash.
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash.
另請參閱 constBegin () 和 end ().
返迴迭代器指嚮的項具有 key 在哈希中。
若哈希包含的項不具有 key ,函數返迴 constEnd ().
該函數在 Qt 4.1 引入。
另請參閱 find () 和 QMultiHash::constFind ().
Returns true if the hash contains an item with the key ;否則返迴 false。
另請參閱 count () 和 QMultiHash::contains ().
返迴項數關聯 key .
另請參閱 contains () 和 insertMulti ().
這是重載函數。
如同 size ().
此函數為兼容 STL (標準模闆庫) 提供。它相當於 isEmpty (), returning true if the hash is empty; otherwise returns false.
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash.
這是重載函數。
Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator to the next item in the hash.
不像 remove () 和 take (), this function never causes QHash to rehash its internal data structure. This means that it can safely be called while iterating, and won't affect the order of items in the hash. For example:
QHash<QObject *, int> objectHash; ... QHash<QObject *, int>::iterator i = objectHash.find(obj); while (i != objectHash.end() && i.key() == obj) { if (i.value() == 0) { i = objectHash.erase(i); } else { ++i; } }
另請參閱 remove (), take (),和 find ().
返迴迭代器指嚮的項具有 key 在哈希中。
若哈希包含的項不具有 key ,函數返迴 end ().
If the hash contains multiple items with the key , this function returns an iterator that points to the most recently inserted value. The other values are accessible by incrementing the iterator. For example, here's some code that iterates over all the items with the same key:
QHash<QString, int> hash; ... QHash<QString, int>::const_iterator i = hash.find("HDR"); while (i != hash.end() && i.key() == "HDR") { cout << i.value() << endl; ++i; }
另請參閱 value (), values (),和 QMultiHash::find ().
這是重載函數。
插入新項具有 key 和值 value .
If there is already an item with the key , that item's value is replaced with value .
If there are multiple items with the key , the most recently inserted item's value is replaced with value .
另請參閱 insertMulti ().
插入新項具有 key 和值 value .
If there is already an item with the same key in the hash, this function will simply create a new one. (This behavior is different from insert (), which overwrites the value of an existing item.)
Returns true if the hash contains no items; otherwise returns false.
另請參閱 size ().
Returns the first key mapped to value .
若哈希包含的項不具有 value , the function returns a default-constructed key.
此函數可能很慢 ( 綫性時間 ),因為 QHash 's internal data structure is optimized for fast lookup by key, not by value.
這是重載函數。
Returns the first key mapped to value ,或 defaultKey if the hash contains no item mapped to value .
此函數可能很慢 ( 綫性時間 ),因為 QHash 's internal data structure is optimized for fast lookup by key, not by value.
該函數在 Qt 4.3 引入。
Returns a list containing all the keys in the hash, in an arbitrary order. Keys that occur multiple times in the hash (because items were inserted with insertMulti (),或 unite () was used) also occur multiple times in the list.
To obtain a list of unique keys, where each key from the map only occurs once, use uniqueKeys ().
The order is guaranteed to be the same as that used by values ().
另請參閱 uniqueKeys (), values (),和 key ().
這是重載函數。
Returns a list containing all the keys associated with value value ,按任意次序。
此函數可能很慢 ( 綫性時間 ),因為 QHash 's internal data structure is optimized for fast lookup by key, not by value.
Removes all the items that have the key from the hash. Returns the number of items removed which is usually 1 but will be 0 if the key isn't in the hash, or greater than 1 if insertMulti () has been used with the key .
另請參閱 clear (), take (),和 QMultiHash::remove ().
確保 QHash 's internal hash table consists of at least size buckets.
This function is useful for code that needs to build a huge hash and wants to avoid repeated reallocation. For example:
QHash<QString, int> hash; hash.reserve(20000); for (int i = 0; i < 20000; ++i) hash.insert(keys[i], values[i]);
Ideally, size should be slightly more than the maximum number of items expected in the hash. size doesn't have to be prime, because QHash will use a prime number internally anyway. If size is an underestimate, the worst that will happen is that the QHash will be a bit slower.
In general, you will rarely ever need to call this function. QHash 's internal hash table automatically shrinks or grows to provide good performance without wasting too much memory.
另請參閱 squeeze () 和 capacity ().
返迴哈希中的項數。
Reduces the size of the QHash 's internal hash table to save memory.
The sole purpose of this function is to provide a means of fine tuning QHash 's memory usage. In general, you will rarely ever need to call this function.
另請參閱 reserve () 和 capacity ().
交換哈希 other with this hash. This operation is very fast and never fails.
該函數在 Qt 4.8 引入。
Removes the item with the key from the hash and returns the value associated with it.
If the item does not exist in the hash, the function simply returns a default-constructed value. If there are multiple items for key in the hash, only the most recently inserted one is removed.
若不使用返迴值, remove () 效率更高。
另請參閱 remove ().
Returns a list containing all the keys in the map. Keys that occur multiple times in the map (because items were inserted with insertMulti (),或 unite () was used) occur only once in the returned list.
該函數在 Qt 4.2 引入。
Inserts all the items in the other hash into this hash. If a key is common to both hashes, the resulting hash will contain the key multiple times.
另請參閱 insertMulti ().
返迴值關聯 key .
若哈希包含的項不具有 key , the function returns a default-constructed value. If there are multiple items for the key in the hash, the value of the most recently inserted one is returned.
另請參閱 key (), values (), contains (),和 operator[] ().
這是重載函數。
If the hash contains no item with the given key ,函數返迴 defaultValue .
Returns a list containing all the values in the hash, in an arbitrary order. If a key is associated multiple values, all of its values will be in the list, and not just the most recently inserted one.
The order is guaranteed to be the same as that used by keys ().
這是重載函數。
Returns a list of all the values associated with the key , from the most recently inserted to the least recently inserted.
另請參閱 count () 和 insertMulti ().
返迴 true 若 other is not equal to this hash; otherwise returns false.
Two hashes are considered equal if they contain the same (key, value) pairs.
This function requires the value type to implement
operator==()
.
另請參閱 operator== ().
賦值 other to this hash and returns a reference to this hash.
返迴 true 若 other is equal to this hash; otherwise returns false.
Two hashes are considered equal if they contain the same (key, value) pairs.
This function requires the value type to implement
operator==()
.
另請參閱 operator!= ().
返迴值關聯 key 作為可修改引用。
若哈希包含的項不具有 key , the function inserts a default-constructed value into the hash with the key , and returns a reference to it. If the hash contains multiple items with the key , this function returns a reference to the most recently inserted value.
這是重載函數。
如同 value ().
Computes a hash key from the QXmlNodeModelIndex index , and returns it. This function would be used by QHash if you wanted to build a hash table for instances of QXmlNodeModelIndex .
The hash is computed on QXmlNodeModelIndex::data (), QXmlNodeModelIndex::additionalData (),和 QXmlNodeModelIndex::model (). This means the hash key can be used for node indexes from different node models.
該函數在 Qt 4.4 引入。
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
返迴哈希值為 key .
類型
T1
and
T2
must be supported by
qHash
().
該函數在 Qt 4.3 引入。
寫入哈希 hash 到流 out .
This function requires the key and value types to implement
operator<<()
.
另請參閱 序列化 Qt 數據類型 .
讀取哈希從流 in into hash .
This function requires the key and value types to implement
operator>>()
.
另請參閱 序列化 Qt 數據類型 .