QMultiMap 類

模闆 <typename Key, typename T> 類 QMultiMap

The QMultiMap class is a template class that provides an associative array with multiple equivalent keys. 更多...

頭: #include <QMultiMap>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

注意: 此類的所有函數 可重入 .

公共類型

class const_iterator
class iterator
class key_iterator
ConstIterator
Iterator
const_key_value_iterator
difference_type
key_type
key_value_iterator
mapped_type
size_type

公共函數

QMultiMap ()
QMultiMap (std::initializer_list<std::pair<Key, T>> list )
QMultiMap (const QMap<Key, T> & other )
QMultiMap (QMap<Key, T> && other )
QMultiMap (const std::multimap<Key, T> & other )
QMultiMap (std::multimap<Key, T> && other )
QMultiMap (QMultiMap<Key, T> && other )
QMultiMap (const QMultiMap<Key, T> & other )
~QMultiMap ()
auto asKeyValueRange () &
auto asKeyValueRange () const &
auto asKeyValueRange () &&
auto asKeyValueRange () const &&
QMultiMap::iterator begin ()
QMultiMap::const_iterator begin () const
QMultiMap::const_iterator cbegin () const
QMultiMap::const_iterator cend () const
void clear ()
QMultiMap::const_iterator constBegin () const
QMultiMap::const_iterator constEnd () const
QMultiMap::const_iterator constFind (const Key & key ) const
QMultiMap::const_iterator constFind (const Key & key , const T & value ) const
QMultiMap::const_key_value_iterator constKeyValueBegin () const
QMultiMap::const_key_value_iterator constKeyValueEnd () const
bool contains (const Key & key ) const
bool contains (const Key & key , const T & value ) const
QMultiMap::size_type count (const Key & key ) const
QMultiMap::size_type count (const Key & key , const T & value ) const
QMultiMap::size_type count () const
bool empty () const
QMultiMap::iterator end ()
QMultiMap::const_iterator end () const
QPair<QMultiMap::iterator, QMultiMap::iterator> equal_range (const Key & key )
QPair<QMultiMap::const_iterator, QMultiMap::const_iterator> equal_range (const Key & key ) const
QMultiMap::iterator erase (QMultiMap::const_iterator pos )
QMultiMap::iterator erase (QMultiMap::const_iterator first , QMultiMap::const_iterator last )
QMultiMap::iterator find (const Key & key )
QMultiMap::const_iterator find (const Key & key ) const
QMultiMap::const_iterator find (const Key & key , const T & value ) const
T & first ()
const T & first () const
const Key & firstKey () const
QMultiMap::iterator insert (const Key & key , const T & value )
QMultiMap::iterator insert (QMultiMap::const_iterator pos , const Key & key , const T & value )
bool isEmpty () const
Key key (const T & value , const Key & defaultKey = Key()) const
QMultiMap::key_iterator keyBegin () const
QMultiMap::key_iterator keyEnd () const
QMultiMap::key_value_iterator keyValueBegin ()
QMultiMap::const_key_value_iterator keyValueBegin () const
QMultiMap::key_value_iterator keyValueEnd ()
QMultiMap::const_key_value_iterator keyValueEnd () const
QList<Key> keys () const
QList<Key> keys (const T & value ) const
T & last ()
const T & last () const
const Key & lastKey () const
QMultiMap::iterator lowerBound (const Key & key )
QMultiMap::const_iterator lowerBound (const Key & key ) const
QMultiMap::size_type remove (const Key & key )
QMultiMap::size_type remove (const Key & key , const T & value )
QMultiMap::size_type removeIf (Predicate pred )
QMultiMap::iterator replace (const Key & key , const T & value )
QMultiMap::size_type size () const
void swap (QMultiMap<Key, T> & other )
T take (const Key & key )
std::multimap<Key, T> toStdMultiMap () const &
QList<Key> uniqueKeys () const
QMultiMap<Key, T> & unite (const QMultiMap<Key, T> & other )
QMultiMap<Key, T> & unite (QMultiMap<Key, T> && other )
QMultiMap::iterator upperBound (const Key & key )
QMultiMap::const_iterator upperBound (const Key & key ) const
T value (const Key & key , const T & defaultValue = T()) const
QList<T> values () const
QList<T> values (const Key & key ) const
QMultiMap<Key, T> & operator= (const QMultiMap<Key, T> & other )
QMultiMap<Key, T> & operator= (QMultiMap<Key, T> && other )
qsizetype erase_if (QMultiMap<Key, T> & map , Predicate pred )
bool operator!= (const QMultiMap<Key, T> & lhs , const QMultiMap<Key, T> & rhs )
QMultiMap<Key, T> operator+ (const QMultiMap<Key, T> & lhs , const QMultiMap<Key, T> & rhs )
QMultiMap<Key, T> operator+= (QMultiMap<Key, T> & lhs , const QMultiMap<Key, T> & rhs )
QDataStream & operator<< (QDataStream & out , const QMultiMap<Key, T> & map )
bool operator== (const QMultiMap<Key, T> & lhs , const QMultiMap<Key, T> & rhs )
QDataStream & operator>> (QDataStream & in , QMultiMap<Key, T> & map )

詳細描述

QMultiMap<Key, T> 是一種 Qt 一般 容器類 . It stores (key, value) pairs and provides fast lookup by key.

QMultiMap and QMultiHash 提供非常類似的功能。差異是:

  • QMultiHash provides average faster lookups than QMultiMap. (See 算法的復雜性 瞭解細節。)
  • 當遍曆 QMultiHash , the items are arbitrarily ordered. With QMultiMap, the items are always sorted by key.
  • Key 類型對於 QMultiHash 必須提供 operator==() 和全局 qHash (Key) function. The key type of a QMultiMap must provide operator<() specifying a total order. Since Qt 5.8.1 it is also safe to use a pointer type as key, even if the underlying operator<() does not provide a total order.

Here's an example QMultiMap with QString 鍵和 int 值:

QMultiMap<QString, int> multimap;
					

To insert a (key, value) pair into the multi map, you can use insert ():

multimap.insert("a", 1);
multimap.insert("b", 3);
multimap.insert("c", 7);
multimap.insert("c", -5);
					

This inserts the following three (key, value) pairs into the QMultiMap: ("a", 1), ("b", 3), ("c", 7), and ("c", -5); note that duplicate keys are allowed.

To look up a value, use find () 或 value ():

int num2 = multimap.value("a"); // 1
int num3 = multimap.value("thirteen"); // not found; 0
int num3 = 0;
auto it = multimap.constFind("b");
if (it != multimap.cend()) {
    num3 = it.value();
}
					

If there is no item with the specified key in the map, these functions return a 默認構造值 .

If you want to check whether the map contains a certain key, use contains ():

int timeout = 30;
if (multimap.contains("TIMEOUT"))
    timeout = multimap.value("TIMEOUT");
// better:
auto it = multimap.find("TIMEOUT");
if (it != multimap.end())
    timeout = it.value();
					

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 = multimap.value("TIMEOUT", 30);
					

If you want to navigate through all the (key, value) pairs stored in a QMultiMap, you can use an iterator. QMultiMap provides both Java 風格迭代器 ( QMultiMapIterator and QMutableMultiMapIterator ) 和 STL 樣式迭代器 ( QMultiMap::const_iterator and QMultiMap::iterator ). Here's how to iterate over a QMultiMap< QString , int> using a Java-style iterator:

QMultiMapIterator<QString, int> i(multimap);
while (i.hasNext()) {
    i.next();
    cout << qPrintable(i.key()) << ": " << i.value() << endl;
}
					

Here's the same code, but using an STL-style iterator this time:

for (auto i = multimap.cbegin(), end = multimap.cend(); i != end; ++i)
    cout << qPrintable(i.key()) << ": " << i.value() << endl;
					

The items are traversed in ascending key order.

A QMultiMap allows multiple values per key. If you call insert () with a key that already exists in the map, a new (key, value) pair will be inserted. For example:

multimap.insert("plenty", 100);
multimap.insert("plenty", 2000);
// multimap.size() == 2
					

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 = multimap.values("plenty");
for (auto i : std::as_const(values))
    cout << i << endl;
					

The items that share the same key are available from most recently to least recently inserted. Another approach is to call find () to get the STL-style iterator for the first item with a key and iterate from there:

auto i = multimap.find("plenty");
while (i != map.end() && i.key() == "plenty") {
    cout << i.value() << endl;
    ++i;
}
// better:
auto [i, end] = multimap.equal_range("plenty");
while (i != end) {
    cout << i.value() << endl;
    ++i;
}
					

If you only need to extract the values from a map (not the keys), you can also use range-based for:

QMap<QString, int> multimap;
...
for (int value : std::as_const(multimap))
    cout << value << endl;
					

Items can be removed from the multi map in several ways. One way is to call remove (); this will remove any item with the given key. Another way is to use QMutableMultiMapIterator::remove (). In addition, you can clear the entire map using clear ().

It is possible to merge two multi maps by calling unite (), by using operator+(), and by using operator+=(). Example:

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
					

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<(). QMap uses it to keep its items sorted, and assumes that two keys x and y are equal if neither x < y nor y < x 為 true。

範例:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
class Employee
{
public:
    Employee() {}
    Employee(const QString &name, QDate dateOfBirth);
    ...
private:
    QString myName;
    QDate myDateOfBirth;
};
inline bool operator<(const Employee &e1, const Employee &e2)
{
    if (e1.name() != e2.name())
        return e1.name() < e2.name();
    return e1.dateOfBirth() < e2.dateOfBirth();
}
#endif // EMPLOYEE_H
					

In the example, we start by comparing the employees' names. If they're equal, we compare their dates of birth to break the tie.

另請參閱 QMultiMapIterator , QMutableMultiMapIterator ,和 QMultiHash .

成員類型文檔編製

QMultiMap:: ConstIterator

Qt 樣式同義詞 QMultiMap::const_iterator .

QMultiMap:: Iterator

Qt 樣式同義詞 QMultiMap::iterator .

QMultiMap:: const_key_value_iterator

The QMultiMap::const_key_value_iterator typedef provides an STL-style iterator for QMultiMap .

QMultiMap::const_key_value_iterator is essentially the same as QMultiMap::const_iterator with the difference that operator*() returns a key/value pair instead of a value.

另請參閱 QKeyValueIterator .

[alias] QMultiMap:: difference_type

typedef 對於 ptrdiff_t。為兼容 STL 提供。

[alias] QMultiMap:: key_type

typedef 對於 Key。為兼容 STL 提供。

QMultiMap:: key_value_iterator

The QMultiMap::key_value_iterator typedef provides an STL-style iterator for QMultiMap .

QMultiMap::key_value_iterator is essentially the same as QMultiMap::iterator with the difference that operator*() returns a key/value pair instead of a value.

另請參閱 QKeyValueIterator .

[alias] QMultiMap:: mapped_type

typedef 對於 T。為兼容 STL 提供。

[alias] QMultiMap:: size_type

typedef 對於 int。為兼容 STL 提供。

成員函數文檔編製

[since 6.4] auto QMultiMap:: asKeyValueRange () &

[since 6.4] auto QMultiMap:: asKeyValueRange () &&

[since 6.4] auto QMultiMap:: asKeyValueRange () const &

[since 6.4] auto QMultiMap:: asKeyValueRange () const &&

Returns a range object that allows iteration over this multi map as key/value pairs. For instance, this range object can be used in a range-based for loop, in combination with a structured binding declaration:

QMultiMap<QString, int> map;
map.insert("January", 1);
map.insert("February", 2);
// ...
map.insert("December", 12);
for (auto [key, value] : map.asKeyValueRange()) {
    cout << qPrintable(key) << ": " << value << endl;
    --value; // convert to JS month indexing
}
					

Note that both the key and the value obtained this way are references to the ones in the multi map. Specifically, mutating the value will modify the map itself.

This function was introduced in Qt 6.4.

另請參閱 QKeyValueIterator .

QMultiMap:: QMultiMap ()

Constructs an empty multi map.

另請參閱 clear ().

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 .

[explicit, since 6.0] QMultiMap:: QMultiMap (const QMap < Key , T > & other )

Constructs a multi map as a copy of other .

該函數在 Qt 6.0 引入。

[explicit, since 6.0] QMultiMap:: QMultiMap ( QMap < Key , T > && other )

other is shared, constructs a multi map as a copy of other . Otherwise, constructs a multi map by moving the elements from other .

該函數在 Qt 6.0 引入。

[explicit] QMultiMap:: QMultiMap (const std::multimap < Key , T > & other )

構造副本為 other .

另請參閱 toStdMultiMap ().

[explicit] QMultiMap:: QMultiMap ( std::multimap < Key , T > && other )

Constructs a multi map by moving from other .

另請參閱 toStdMultiMap ().

[default] QMultiMap:: QMultiMap ( QMultiMap < Key , T > && other )

Move-constructs a QMultiMap instance, making it point at the same object that other 所指嚮的。

[default] QMultiMap:: QMultiMap (const QMultiMap < Key , T > & other )

構造副本為 other .

This operation occurs in 常量時間 , because QMultiMap is 隱式共享 . This makes returning a QMultiMap from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takes 綫性時間 .

另請參閱 operator= ().

[default] QMultiMap:: ~QMultiMap ()

Destroys the multi map. References to the values in the multi map, and all iterators over this multi map, become invalid.

QMultiMap::iterator QMultiMap:: begin ()

返迴 STL 樣式迭代器 pointing to the first item in the multi map.

另請參閱 constBegin () 和 end ().

QMultiMap::const_iterator QMultiMap:: begin () const

這是重載函數。

QMultiMap::const_iterator QMultiMap:: cbegin () const

返迴常量 STL 樣式迭代器 pointing to the first item in the multi map.

另請參閱 begin () 和 cend ().

QMultiMap::const_iterator QMultiMap:: cend () const

返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last item in the multi map.

另請參閱 cbegin () 和 end ().

void QMultiMap:: clear ()

Removes all items from the multi map.

另請參閱 remove ().

QMultiMap::const_iterator QMultiMap:: constBegin () const

返迴常量 STL 樣式迭代器 pointing to the first item in the multi map.

另請參閱 begin () 和 constEnd ().

QMultiMap::const_iterator QMultiMap:: constEnd () const

返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last item in the multi map.

另請參閱 constBegin () 和 end ().

QMultiMap::const_iterator QMultiMap:: constFind (const Key & key ) const

Returns an const iterator pointing to the item with key key in the multi map.

If the multi map contains no item with key key ,函數返迴 constEnd ().

另請參閱 find () and QMultiMap::constFind().

QMultiMap::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 ().

另請參閱 QMap::constFind ().

QMultiMap::const_key_value_iterator QMultiMap:: constKeyValueBegin () const

返迴常量 STL 樣式迭代器 pointing to the first entry in the multi map.

另請參閱 keyValueBegin ().

QMultiMap::const_key_value_iterator QMultiMap:: constKeyValueEnd () const

返迴常量 STL 樣式迭代器 pointing to the imaginary entry after the last entry in the multi map.

另請參閱 constKeyValueBegin ().

bool QMultiMap:: contains (const Key & key ) const

返迴 true if the multi map contains an item with key key ;否則返迴 false .

另請參閱 count ().

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

返迴 true if the multi map contains an item with key key and value value ;否則返迴 false .

另請參閱 count ().

QMultiMap::size_type QMultiMap:: count (const Key & key ) const

Returns the number of items associated with key key .

另請參閱 contains () 和 QMultiMap::count ().

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

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

另請參閱 contains () 和 QMultiMap::count ().

QMultiMap::size_type QMultiMap:: count () const

這是重載函數。

如同 size ().

bool QMultiMap:: empty () const

此函數為兼容 STL (標準模闆庫) 提供。它相當於 isEmpty (), returning true if the map is empty; otherwise returning false.

QMultiMap::iterator QMultiMap:: end ()

返迴 STL 樣式迭代器 pointing to the imaginary item after the last item in the multi map.

另請參閱 begin () 和 constEnd ().

QMultiMap::const_iterator QMultiMap:: end () const

這是重載函數。

QPair < QMultiMap::iterator , QMultiMap::iterator > QMultiMap:: equal_range (const Key & key )

Returns a pair of iterators delimiting the range of values [first, second) , that are stored under key .

QPair < QMultiMap::const_iterator , QMultiMap::const_iterator > QMultiMap:: equal_range (const Key & key ) const

這是重載函數。

QMultiMap::iterator QMultiMap:: erase ( QMultiMap::const_iterator pos )

Removes the (key, value) pair pointed to by the iterator pos from the multi map, and returns an iterator to the next item in the map.

注意: The iterator pos must be valid and dereferenceable.

另請參閱 remove ().

[since 6.0] QMultiMap::iterator QMultiMap:: erase ( QMultiMap::const_iterator first , QMultiMap::const_iterator last )

Removes the (key, value) pairs pointed to by the iterator range [ first , last ) from the multi map. Returns an iterator to the item in the multi map following the last removed element.

注意: 範圍 [first, last) must be a valid range in *this .

該函數在 Qt 6.0 引入。

另請參閱 remove ().

QMultiMap::iterator QMultiMap:: find (const Key & key )

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

If the multi map contains no item with key key ,函數返迴 end ().

If the map contains multiple items with key 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:

auto i = multimap.find("plenty");
while (i != map.end() && i.key() == "plenty") {
    cout << i.value() << endl;
    ++i;
}
// better:
auto [i, end] = multimap.equal_range("plenty");
while (i != end) {
    cout << i.value() << endl;
    ++i;
}
					

另請參閱 constFind (), value (), values (), lowerBound (),和 upperBound ().

QMultiMap::const_iterator QMultiMap:: find (const Key & key ) const

這是重載函數。

QMultiMap::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.

T &QMultiMap:: first ()

Returns a reference to the first value in the multi map, that is the value mapped to the smallest key. This function assumes that the multi map is not empty.

When unshared (or const version is called), this executes in 常量時間 .

另請參閱 last (), firstKey (),和 isEmpty ().

const T &QMultiMap:: first () const

這是重載函數。

const Key &QMultiMap:: firstKey () const

Returns a reference to the smallest key in the multi map. This function assumes that the multi map is not empty.

This executes in 常量時間 .

另請參閱 lastKey (), first (), keyBegin (),和 isEmpty ().

QMultiMap::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 ().

QMultiMap::iterator QMultiMap:: insert ( QMultiMap::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 multi map while constEnd () suggests that the key is (strictly) larger than any key in the multi 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 the hint is correct and the multi map is unshared, the insert executes in amortized 常量時間 .

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

When creating a multi map from sorted data inserting the largest key first with constBegin () is faster than inserting in sorted order with constEnd (), since constEnd () - 1 (which is needed to check if the hint is valid) needs logarithmic time .

注意: 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 multi map and the pos multi map.

bool QMultiMap:: isEmpty () const

返迴 true if the multi map contains no items; otherwise returns false.

另請參閱 size ().

Key QMultiMap:: key (const T & value , const Key & defaultKey = Key()) const

這是重載函數。

返迴第 1 個鍵具有值 value ,或 defaultKey if the multi map contains no item with value value . If no defaultKey is provided the function returns a default-constructed key .

此函數可能很慢 ( 綫性時間 ),因為 QMultiMap 's internal data structure is optimized for fast lookup by key, not by value.

另請參閱 value () 和 keys ().

QMultiMap::key_iterator QMultiMap:: keyBegin () const

返迴常量 STL 樣式迭代器 pointing to the first key in the multi map.

另請參閱 keyEnd () 和 firstKey ().

QMultiMap::key_iterator QMultiMap:: keyEnd () const

返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last key in the multi map.

另請參閱 keyBegin () 和 lastKey ().

QMultiMap::key_value_iterator QMultiMap:: keyValueBegin ()

返迴 STL 樣式迭代器 pointing to the first entry in the multi map.

另請參閱 keyValueEnd ().

QMultiMap::const_key_value_iterator QMultiMap:: keyValueBegin () const

返迴常量 STL 樣式迭代器 pointing to the first entry in the multi map.

另請參閱 keyValueEnd ().

QMultiMap::key_value_iterator QMultiMap:: keyValueEnd ()

返迴 STL 樣式迭代器 pointing to the imaginary entry after the last entry in the multi map.

另請參閱 keyValueBegin ().

QMultiMap::const_key_value_iterator QMultiMap:: keyValueEnd () const

返迴常量 STL 樣式迭代器 pointing to the imaginary entry after the last entry in the multi map.

另請參閱 keyValueBegin ().

QList < Key > QMultiMap:: keys () const

Returns a list containing all the keys in the multi map in ascending order. Keys that occur multiple times in the multi map also occur multiple times in the list.

The order is guaranteed to be the same as that used by values ().

This function creates a new list, in 綫性時間 . The time and memory use that entails can be avoided by iterating from keyBegin () 到 keyEnd ().

另請參閱 values () 和 key ().

QList < Key > QMultiMap:: keys (const T & value ) const

這是重載函數。

Returns a list containing all the keys associated with value value in ascending order.

此函數可能很慢 ( 綫性時間 ),因為 QMultiMap 's internal data structure is optimized for fast lookup by key, not by value.

T &QMultiMap:: last ()

Returns a reference to the last value in the multi map, that is the value mapped to the largest key. This function assumes that the map is not empty.

When unshared (or const version is called), this executes in logarithmic time .

另請參閱 first (), lastKey (),和 isEmpty ().

const T &QMultiMap:: last () const

這是重載函數。

const Key &QMultiMap:: lastKey () const

Returns a reference to the largest key in the multi map. This function assumes that the multi map is not empty.

This executes in logarithmic time .

另請參閱 firstKey (), last (), keyEnd (),和 isEmpty ().

QMultiMap::iterator QMultiMap:: lowerBound (const Key & key )

Returns an iterator pointing to the first item with key key in the map. If the map contains no item with key key , the function returns an iterator to the nearest item with a greater key.

範例:

QMultiMap<int, QString> multimap;
multimap.insert(1, "one");
multimap.insert(5, "five");
multimap.insert(5, "five (2)");
multimap.insert(10, "ten");
multimap.lowerBound(0);      // returns iterator to (1, "one")
multimap.lowerBound(1);      // returns iterator to (1, "one")
multimap.lowerBound(2);      // returns iterator to (5, "five")
multimap.lowerBound(5);      // returns iterator to (5, "five")
multimap.lowerBound(6);      // returns iterator to (10, "ten")
multimap.lowerBound(10);     // returns iterator to (10, "ten")
multimap.lowerBound(999);    // returns end()
					

If the map contains multiple items with key 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:

QMap<QString, int> multimap;
...
QMap<QString, int>::const_iterator i = multimap.lowerBound("HDR");
QMap<QString, int>::const_iterator upperBound = multimap.upperBound("HDR");
while (i != upperBound) {
    cout << i.value() << endl;
    ++i;
}
					

另請參閱 upperBound () 和 find ().

QMultiMap::const_iterator QMultiMap:: lowerBound (const Key & key ) const

這是重載函數。

QMultiMap::size_type QMultiMap:: remove (const Key & key )

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

另請參閱 clear () 和 take ().

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

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

另請參閱 clear () 和 take ().

[since 6.1] template <typename Predicate> QMultiMap::size_type QMultiMap:: removeIf ( Predicate pred )

Removes all elements for which the predicate pred returns true from the multi map.

The function supports predicates which take either an argument of type QMultiMap<Key, T>::iterator , or an argument of type std::pair<const Key &, T &> .

Returns the number of elements removed, if any.

This function was introduced in Qt 6.1.

另請參閱 clear () 和 take ().

QMultiMap::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 ().

QMultiMap::size_type QMultiMap:: size () const

Returns the number of (key, value) pairs in the multi map.

另請參閱 isEmpty () 和 count ().

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

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

T QMultiMap:: take (const Key & key )

Removes the item with the key key from the multi map and returns the value associated with it.

If the item does not exist in the multi map, the function simply returns a 默認構造值 . If there are multiple items for key in the map, only the most recently inserted one is removed and returned.

若不使用返迴值, remove () 效率更高。

另請參閱 remove ().

std::multimap < Key , T > QMultiMap:: toStdMultiMap () const &

Returns an STL multi map equivalent to this QMultiMap .

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.

QMultiMap < Key , T > &QMultiMap:: unite (const QMultiMap < Key , T > & 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.

QMultiMap < Key , T > &QMultiMap:: unite ( QMultiMap < Key , T > && other )

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

other is shared, then the items will be copied instead.

QMultiMap::iterator QMultiMap:: upperBound (const Key & key )

Returns an iterator pointing to the item that immediately follows the last item with key key in the map. If the map contains no item with key key , the function returns an iterator to the nearest item with a greater key.

範例:

QMultiMap<int, QString> multimap;
multimap.insert(1, "one");
multimap.insert(5, "five");
multimap.insert(5, "five (2)");
multimap.insert(10, "ten");
multimap.upperBound(0);      // returns iterator to (1, "one")
multimap.upperBound(1);      // returns iterator to (5, "five")
multimap.upperBound(2);      // returns iterator to (5, "five")
multimap.lowerBound(5);      // returns iterator to (5, "five (2)")
multimap.lowerBound(6);      // returns iterator to (10, "ten")
multimap.upperBound(10);     // returns end()
multimap.upperBound(999);    // returns end()
					

另請參閱 lowerBound () 和 find ().

QMultiMap::const_iterator QMultiMap:: upperBound (const Key & key ) const

這是重載函數。

T QMultiMap:: value (const Key & key , const T & defaultValue = T()) const

Returns the value associated with the key key .

If the multi map contains no item with key key ,函數返迴 defaultValue . If no defaultValue is specified, the function returns a 默認構造值 . If there are multiple items for key in the multi map, the value of the most recently inserted one is returned.

另請參閱 key (), values (),和 contains ().

QList < T > QMultiMap:: values () const

Returns a list containing all the values in the map, in ascending order of their keys. If a key is associated with multiple values, all of its values will be in the list, and not just the most recently inserted one.

另請參閱 keys () 和 value ().

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.

另請參閱 keys () 和 value ().

[default] QMultiMap < Key , T > &QMultiMap:: operator= (const QMultiMap < Key , T > & other )

賦值 other to this multi map and returns a reference to this multi map.

[default] QMultiMap < Key , T > &QMultiMap:: operator= ( QMultiMap < Key , T > && other )

移動賦值 other 到此 QMultiMap 實例。

相關非成員

[since 6.1] template <typename Key, typename T, typename Predicate> qsizetype erase_if ( QMultiMap < Key , T > & map , Predicate pred )

Removes all elements for which the predicate pred returns true from the multi map map .

The function supports predicates which take either an argument of type QMultiMap<Key, T>::iterator , or an argument of type std::pair<const Key &, T &> .

Returns the number of elements removed, if any.

This function was introduced in Qt 6.1.

bool operator!= (const QMultiMap < Key , T > & lhs , const QMultiMap < Key , T > & rhs )

返迴 true if lhs 不等於 rhs ;否則返迴 false .

Two multi maps are considered equal if they contain the same (key, value) pairs, in the same order (which matters for duplicate keys).

This function requires the key and the value types to implement operator==() .

另請參閱 operator== ().

template <typename Key, typename T> QMultiMap < Key , T > operator+ (const QMultiMap < Key , T > & lhs , const QMultiMap < Key , T > & rhs )

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

另請參閱 operator+= ().

template <typename Key, typename T> QMultiMap < Key , T > operator+= ( QMultiMap < Key , T > & lhs , const QMultiMap < Key , T > & rhs )

Inserts all the items in the rhs map into the lhs map and returns the resulting map.

另請參閱 insert () 和 operator+ ().

template <typename Key, typename T> QDataStream & operator<< ( QDataStream & out , const QMultiMap < Key , T > & map )

Writes the multi map map 到流 out .

This function requires the key and value types to implement operator<<() .

另請參閱 QDataStream 運算符格式 .

bool operator== (const QMultiMap < Key , T > & lhs , const QMultiMap < Key , T > & rhs )

返迴 true if lhs 等於 rhs ;否則返迴 false。

Two multi maps are considered equal if they contain the same (key, value) pairs, in the same order (which matters for duplicate keys).

This function requires the key and the value types to implement operator==() .

另請參閱 operator!= ().

template <typename Key, typename T> QDataStream & operator>> ( QDataStream & in , QMultiMap < Key , T > & map )

讀取映射從流 in into map .

This function requires the key and value types to implement operator>>() .

另請參閱 QDataStream 運算符格式 .