QMultiMap 类

模板 <typename Key, typename T> 类 QMultiMap

QMultiMap 类是方便 QMap 子类提供多值映射。 更多...

头: #include <QMultiMap>
qmake: QT += core
继承: QMap

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

公共函数

QMultiMap (const QMap<Key, T> & other )
QMultiMap (std::initializer_list<std::pair<Key, T>> list )
QMultiMap ()
typename QMap<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 QMap<Key, T>::iterator find (const Key & key , const T & value )
typename QMap<Key, T>::const_iterator find (const Key & key , const T & value ) const
typename QMap<Key, T>::iterator insert (const Key & key , const T & value )
typename QMap<Key, T>::iterator insert (typename QMap<Key, T>::const_iterator pos , const Key & key , const T & value )
int remove (const Key & key , const T & value )
typename QMap<Key, T>::iterator replace (const Key & key , const T & value )
void swap (QMultiMap<Key, T> & other )
QList<Key> uniqueKeys () const
QMultiMap<K, V> & unite (const QMultiMap<K, V> & other )
QList<T> values (const Key & key ) const
QMultiMap<K, V> operator+ (const QMultiMap<K, V> & other ) const
QMultiMap<K, V> & operator+= (const QMultiMap<K, V> & other )

详细描述

QMultiMap<Key, T> 是一种 Qt 一般 容器类 . It inherits QMap and extends it with a few functions that make it able to store multi-valued maps. A multi-valued map is a map that allows multiple values with the same key; QMap doesn't allow that.

Because QMultiMap inherits QMap , all of QMap 's functionality also applies to QMultiMap. For example, you can use isEmpty () to test whether the map is empty, and you can traverse a QMultiMap using QMap 的迭代器类 (例如: QMapIterator ). But in addition, it provides an insert () function that inserts but does not overwrite any previous value if the key already exists, and a replace () function that corresponds which does overwite an existing value if they key is already in the map. It also provides convenient operator+() and operator+=().

范例:

QMultiMap<QString, int> map1, map2, map3;
map1.insert("plenty", 100);
map1.insert("plenty", 2000);
// map1.size() == 2
map2.insert("plenty", 5000);
// map2.size() == 1
map3 = map1 + map2;
// map3.size() == 3
					

不像 QMap , QMultiMap 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 = map.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.

If you prefer the STL-style iterators, you can call find () to get the iterator for the first item with a key and iterate from there:

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

QMultiMap's key and value data types must be 可赋值数据类型 . This covers most data types you are likely to encounter, but the compiler won't let you, for example, store a QWidget 作为值;取而代之,存储 QWidget *. In addition, QMultiMap's key type must provide operator<(). See the QMap 文档编制了解细节。

另请参阅 QMap , QMapIterator , QMutableMapIterator ,和 QMultiHash .

成员函数文档编制

QMultiMap:: QMultiMap (const QMap < Key , T > & other )

构造副本为 other (which can be a QMap or a QMultiMap).

另请参阅 operator= ().

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

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

This function is only available if the program is being compiled in C++11 mode.

该函数在 Qt 5.1 引入。

QMultiMap:: QMultiMap ()

Constructs an empty map.

typename QMap < Key , T > ::const_iterator QMultiMap:: constFind (const Key & key , const T & value ) const

Returns an iterator pointing to the item with key key and the value value in the map.

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

该函数在 Qt 4.3 引入。

另请参阅 QMap::constFind ().

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

返回 true if the map contains an item with key key and value value ;否则返回 false .

该函数在 Qt 4.3 引入。

另请参阅 QMap::contains ().

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

Returns the number of items with key key and value value .

该函数在 Qt 4.3 引入。

另请参阅 QMap::count ().

typename QMap < Key , T > ::iterator QMultiMap:: find (const Key & key , const T & value )

Returns an iterator pointing to the item with key key and value value in the map.

If the map contains no such item, the function returns end ().

If the map contains multiple items with key key , this function returns an iterator that points to the most recently inserted value.

该函数在 Qt 4.3 引入。

另请参阅 QMap::find ().

typename QMap < Key , T > ::const_iterator QMultiMap:: find (const Key & key , const T & value ) const

这是重载函数。

Returns a const iterator pointing to the item with the given key and value in the map.

If the map contains no such item, the function returns end ().

If the map contains multiple items with the specified key , this function returns a const iterator that points to the most recently inserted value.

该函数在 Qt 4.3 引入。

另请参阅 QMap::find ().

typename QMap < Key , T > ::iterator QMultiMap:: insert (const Key & key , const T & value )

Inserts a new item with the key key 和值 value .

If there is already an item with the same key in the map, this function will simply create a new one. (This behavior is different from replace (), which overwrites the value of an existing item.)

另请参阅 replace ().

typename QMap < Key , T > ::iterator QMultiMap:: insert ( typename QMap < Key , T > ::const_iterator pos , const Key & key , const T & value )

Inserts a new item with the key key and value value and with hint pos suggesting where to do the insert.

constBegin () is used as hint it indicates that the key is less than any key in the map while constEnd () suggests that the key is larger than any key in the map. Otherwise the hint should meet the condition ( pos - 1). key () < key <= pos. key (). If the hint pos is wrong it is ignored and a regular insert is done.

If there is already an item with the same key in the map, this function will simply create a new one.

注意: Be careful with the hint. Providing an iterator from an older shared instance might crash but there is also a risk that it will silently corrupt both the map and the pos map.

该函数在 Qt 5.1 引入。

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

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

该函数在 Qt 4.3 引入。

另请参阅 QMap::remove ().

typename QMap < Key , T > ::iterator QMultiMap:: replace (const Key & key , const T & value )

Inserts a new item with the key key 和值 value .

If there is already an item with the key key , that item's value is replaced with value .

If there are multiple items with the key key , the most recently inserted item's value is replaced with value .

另请参阅 insert ().

void QMultiMap:: swap ( QMultiMap < Key , T > & other )

Swaps map other with this map. This operation is very fast and never fails.

该函数在 Qt 4.8 引入。

QList < Key > QMultiMap:: uniqueKeys () const

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

该函数在 Qt 4.2 引入。

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

Inserts all the items in the other map into this map. If a key is common to both maps, the resulting map will contain the key multiple times.

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

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

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

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

另请参阅 operator+= ().

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

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

另请参阅 insert () 和 operator+ ().