QMultiHash 类

模板 <typename Key, typename T> 类 QMultiHash

QMultiHash 类是方便 QHash 子类,提供多值哈希。 更多...

头: #include <QMultiHash>
qmake: QT += core
继承: QHash

注意: 此类的所有函数 可重入 .

公共函数

QMultiHash (const QHash<Key, T> & other )
QMultiHash (InputIterator begin , InputIterator end )
QMultiHash (std::initializer_list<std::pair<Key, T>> list )
QMultiHash ()
typename QHash<Key, T>::const_iterator constFind (const Key & key , const T & value ) const
bool contains (const Key & key , const T & value ) const
int count (const Key & key , const T & value ) const
typename QHash<Key, T>::iterator find (const Key & key , const T & value )
typename QHash<Key, T>::const_iterator find (const Key & key , const T & value ) const
typename QHash<Key, T>::iterator insert (const Key & key , const T & value )
int remove (const Key & key , const T & value )
typename QHash<Key, T>::iterator replace (const Key & key , const T & value )
void swap (QMultiHash<K, V> & other )
QList<Key> uniqueKeys () const
QMultiHash<K, V> & unite (const QMultiHash<K, V> & other )
QList<T> values (const Key & key ) const
QMultiHash<K, V> operator+ (const QMultiHash<K, V> & other ) const
QMultiHash<K, V> & operator+= (const QMultiHash<K, V> & other )
uint qHash (const QMultiHash<Key, T> & key , uint seed = 0)

详细描述

QMultiHash<Key, T> 是一种 Qt 一般 容器类 . It inherits QHash and extends it with a few convenience functions that make it more suitable than QHash for storing multi-valued hashes. A multi-valued hash is a hash that allows multiple values with the same key.

Because QMultiHash inherits QHash , all of QHash 's functionality also applies to QMultiHash. For example, you can use isEmpty () to test whether the hash is empty, and you can traverse a QMultiHash using QHash 的迭代器类 (例如: QHashIterator ). But opposed to QHash , it provides an insert () function will allow the insertion of multiple items with the same key. The replace () function corresponds to QHash::insert (). It also provides convenient operator+() and operator+=().

不像 QMultiMap , QMultiHash does not provide and ordering of the inserted items. The only guarantee is that items that share the same key will appear consecutively, from the most recently to the least recently inserted value.

范例:

QMultiHash<QString, int> hash1, hash2, hash3;
hash1.insert("plenty", 100);
hash1.insert("plenty", 2000);
// hash1.size() == 2
hash2.insert("plenty", 5000);
// hash2.size() == 1
hash3 = hash1 + hash2;
// hash3.size() == 3
					

不像 QHash , QMultiHash provides no operator[]. Use value () 或 replace () if you want to access the most recently inserted item with a certain key.

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) << Qt::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 STL-style iterator for the first item with a key and iterate from there:

QMultiHash<QString, int>::iterator i = hash.find("plenty");
while (i != hash.end() && i.key() == "plenty") {
    cout << i.value() << Qt::endl;
    ++i;
}
					

QMultiHash's key and value data types must be 可赋值数据类型 。例如,无法存储 QWidget 作为值;取而代之,存储 QWidget *. In addition, QMultiHash's key type must provide operator==(), and there must also be a qHash () function in the type's namespace that returns a hash value for an argument of the key's type. See the QHash 文档编制了解细节。

另请参阅 QHash , QHashIterator , QMutableHashIterator ,和 QMultiMap .

成员函数文档编制

QMultiHash:: QMultiHash (const QHash < Key , T > & other )

构造副本为 other (which can be a QHash or a QMultiHash).

另请参阅 operator= ().

template <typename InputIterator> QMultiHash:: QMultiHash ( InputIterator begin , InputIterator end )

Constructs a multi-hash with a copy of each of the elements in the iterator range [ begin , end ). Either the elements iterated by the range must be objects with 第一 and second data members (like QPair , std::pair , etc.) convertible to Key and to T respectively; or the iterators must have key() and value() member functions, returning a key convertible to Key and a value convertible to T 分别。

该函数在 Qt 5.14 引入。

QMultiHash:: QMultiHash ( std::initializer_list < std::pair < Key , T >> list )

Constructs a multi-hash with a copy of each of the elements in the initializer list list .

该函数在 Qt 5.1 引入。

QMultiHash:: QMultiHash ()

构造空哈希。

typename QHash < Key , T > ::const_iterator QMultiHash:: constFind (const Key & key , const T & value ) const

返回迭代器指向的项具有 key value 在哈希中。

If the hash contains no such item, the function returns constEnd ().

该函数在 Qt 4.3 引入。

另请参阅 QHash::constFind ().

bool QMultiHash:: contains (const Key & key , const T & value ) const

返回 true 若哈希包含的项具有 key and value ;否则返回 false .

该函数在 Qt 4.3 引入。

另请参阅 QHash::contains ().

int QMultiHash:: count (const Key & key , const T & value ) const

Returns the number of items with the key and value .

该函数在 Qt 4.3 引入。

另请参阅 QHash::count ().

typename QHash < Key , T > ::iterator QMultiHash:: find (const Key & key , const T & value )

返回迭代器指向的项具有 key and value . If the hash contains no such item, the function returns end ().

If the hash contains multiple items with the key and value , the iterator returned points to the most recently inserted item.

该函数在 Qt 4.3 引入。

另请参阅 QHash::find ().

typename QHash < Key , T > ::const_iterator QMultiHash:: find (const Key & key , const T & value ) const

这是重载函数。

该函数在 Qt 4.3 引入。

typename QHash < Key , T > ::iterator QMultiHash:: insert (const Key & key , const T & value )

插入新项具有 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 replace (), which overwrites the value of an existing item.)

另请参阅 replace ().

int QMultiHash:: remove (const Key & key , const T & value )

Removes all the items that have the key and the value value from the hash. Returns the number of items removed.

该函数在 Qt 4.3 引入。

另请参阅 QHash::remove ().

typename QHash < Key , T > ::iterator QMultiHash:: replace (const Key & key , const T & value )

插入新项具有 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 .

另请参阅 insert ().

void QMultiHash:: swap ( QMultiHash < K , V > & other )

交换哈希 other with this hash. This operation is very fast and never fails.

该函数在 Qt 4.8 引入。

QList < Key > QMultiHash:: uniqueKeys () const

Returns a list containing all the keys in the map. Keys that occur multiple times in the map occur only once in the returned list.

该函数在 Qt 5.13 引入。

另请参阅 keys () 和 values ().

QMultiHash < K , V > &QMultiHash:: unite (const QMultiHash < K , V > & other )

Inserts all the items in the other hash into this hash and returns a reference to this hash.

该函数在 Qt 5.13 引入。

另请参阅 insert ().

QList < T > QMultiHash:: values (const Key & key ) const

这是重载函数。

Returns a list of all the values associated with the key , from the most recently inserted to the least recently inserted.

另请参阅 count () 和 insert ().

QMultiHash < K , V > QMultiHash:: operator+ (const QMultiHash < K , V > & other ) const

Returns a hash that contains all the items in this hash in addition to all the items in other . If a key is common to both hashes, the resulting hash will contain the key multiple times.

另请参阅 operator+= ().

QMultiHash < K , V > &QMultiHash:: operator+= (const QMultiHash < K , V > & other )

Inserts all the items in the other hash into this hash and returns a reference to this hash.

另请参阅 unite () 和 insert ().

相关非成员

template <typename Key, typename T> uint qHash (const QMultiHash < Key , T > & key , uint seed = 0)

返回哈希值为 key ,使用 seed 做计算种子。

类型 T must be supported by qHash().

该函数在 Qt 5.8 引入。