QMap 類是提供關聯數組的模闆類。 更多...
| 頭: |
#include <QMap>
|
| 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 |
| QMap () | |
| QMap (const std::map<Key, T> & other ) | |
| QMap (std::initializer_list<std::pair<Key, T>> list ) | |
| QMap (std::map<Key, T> && other ) | |
| QMap (const QMap<Key, T> & other ) | |
| QMap (QMap<Key, T> && other ) | |
| ~QMap () | |
(從 6.4 起)
auto
|
asKeyValueRange () && |
(從 6.4 起)
auto
|
asKeyValueRange () & |
(從 6.4 起)
auto
|
asKeyValueRange () const && |
(從 6.4 起)
auto
|
asKeyValueRange () const & |
| QMap<Key, T>::iterator | begin () |
| QMap<Key, T>::const_iterator | begin () const |
| QMap<Key, T>::const_iterator | cbegin () const |
| QMap<Key, T>::const_iterator | cend () const |
| void | clear () |
| QMap<Key, T>::const_iterator | constBegin () const |
| QMap<Key, T>::const_iterator | constEnd () const |
| QMap<Key, T>::const_iterator | constFind (const Key & key ) const |
| QMap<Key, T>::const_key_value_iterator | constKeyValueBegin () const |
| QMap<Key, T>::const_key_value_iterator | constKeyValueEnd () const |
| bool | contains (const Key & key ) const |
| QMap<Key, T>::size_type | count (const Key & key ) const |
| QMap<Key, T>::size_type | count () const |
| bool | empty () const |
| QMap<Key, T>::iterator | end () |
| QMap<Key, T>::const_iterator | end () const |
| std::pair<QMap<Key, T>::iterator, QMap<Key, T>::iterator> | equal_range (const Key & key ) |
| std::pair<QMap<Key, T>::const_iterator, QMap<Key, T>::const_iterator> | equal_range (const Key & key ) const |
| QMap<Key, T>::iterator | erase (QMap<Key, T>::const_iterator pos ) |
(從 6.0 起)
QMap<Key, T>::iterator
|
erase (QMap<Key, T>::const_iterator first , QMap<Key, T>::const_iterator last ) |
| QMap<Key, T>::iterator | find (const Key & key ) |
| QMap<Key, T>::const_iterator | find (const Key & key ) const |
| T & | first () |
| const T & | first () const |
| const Key & | firstKey () const |
| void | insert (QMap<Key, T> && map ) |
| void | insert (const QMap<Key, T> & map ) |
| QMap<Key, T>::iterator | insert (const Key & key , const T & value ) |
| QMap<Key, T>::iterator | insert (QMap<Key, T>::const_iterator pos , const Key & key , const T & value ) |
| bool | isEmpty () const |
| Key | key (const T & value , const Key & defaultKey = Key()) const |
| QMap<Key, T>::key_iterator | keyBegin () const |
| QMap<Key, T>::key_iterator | keyEnd () const |
| QMap<Key, T>::key_value_iterator | keyValueBegin () |
| QMap<Key, T>::const_key_value_iterator | keyValueBegin () const |
| QMap<Key, T>::key_value_iterator | keyValueEnd () |
| QMap<Key, T>::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 |
| QMap<Key, T>::iterator | lowerBound (const Key & key ) |
| QMap<Key, T>::const_iterator | lowerBound (const Key & key ) const |
| QMap<Key, T>::size_type | remove (const Key & key ) |
(從 6.1 起)
QMap<Key, T>::size_type
|
removeIf (Predicate pred ) |
| QMap<Key, T>::size_type | size () const |
| void | swap (QMap<Key, T> & other ) |
| T | take (const Key & key ) |
| std::map<Key, T> | toStdMap () const & |
(從 6.0 起)
std::map<Key, T>
|
toStdMap () && |
| QMap<Key, T>::iterator | upperBound (const Key & key ) |
| QMap<Key, T>::const_iterator | upperBound (const Key & key ) const |
| T | value (const Key & key , const T & defaultValue = T()) const |
| QList<T> | 值 () const |
| QMap<Key, T> & | operator= (QMap<Key, T> && other ) |
| QMap<Key, T> & | operator= (const QMap<Key, T> & other ) |
| T & | operator[] (const Key & key ) |
| T | operator[] (const Key & key ) const |
(從 6.1 起)
qsizetype
|
erase_if (QMap<Key, T> & map , Predicate pred ) |
(從 6.8 起)
size_t
|
qHash (const QMap<Key, T> & key , size_t seed = 0) |
| bool | operator!= (const QMap<Key, T> & lhs , const QMap<Key, T> & rhs ) |
| QDataStream & | operator<< (QDataStream & out , const QMap<Key, T> & map ) |
| bool | operator== (const QMap<Key, T> & lhs , const QMap<Key, T> & rhs ) |
| QDataStream & | operator>> (QDataStream & in , QMap<Key, T> & map ) |
QMap<Key, T> 是一種 Qt 一般 容器類 。它存儲 (key, value) 對,並提供按 Key 的快速查找。
QMap 和 QHash 提供非常類似的功能。差異是:
這裏是 QMap 範例具有
QString
鍵和
int
值:
QMap<QString, int> map;
要將 (key, value) 對插入映射,可以使用 operator[]():
map["one"] = 1; map["three"] = 3; map["seven"] = 7;
This inserts the following three (key, value) pairs into the QMap: ("one", 1), ("three", 3), and ("seven", 7). Another way to insert items into the map is to use insert ():
map.insert("twelve", 12);
要查找值,使用 operator[]() 或 value ():
int num1 = map["thirteen"]; int num2 = map.value("thirteen");
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 (map.contains("TIMEOUT")) timeout = map.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 = map.value("TIMEOUT", 30);
In general, we recommend that you use contains () 和 value () rather than operator[]() for looking up a key in a map. The reason is that operator[]() silently inserts an item into the map if no item exists with the same key (unless the map is const). For example, the following code snippet will create 1000 items in memory:
// WRONG QMap<int, QWidget *> map; ... for (int i = 0; i < 1000; ++i) { if (map[i] == okButton) cout << "Found button at index " << i << endl; }
要避免此問題,替換
map[i]
with
map.value(i)
在以上代碼中。
If you want to navigate through all the (key, value) pairs stored in a QMap, you can use an iterator. QMap provides both Java 風格迭代器 ( QMapIterator and QMutableMapIterator ) 和 STL 樣式迭代器 ( QMap::const_iterator and QMap::iterator ). Here's how to iterate over a QMap< QString , int> using a Java-style iterator:
QMapIterator<QString, int> i(map); 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 = map.cbegin(), end = map.cend(); i != end; ++i) cout << qPrintable(i.key()) << ": " << i.value() << endl;
The items are traversed in ascending key order.
A QMap allows only one value per key. If you call insert () with a key that already exists in the QMap, the previous value will be erased. For example:
map.insert("plenty", 100); map.insert("plenty", 2000); // map.value("plenty") == 2000
However, you can store multiple values per key by using QMultiMap .
If you only need to extract the values from a map (not the keys), you can also use range-based for:
QMap<QString, int> map; ... for (int value : std::as_const(map)) cout << value << endl;
Items can be removed from the map in several ways. One way is to call remove (); this will remove any item with the given key. Another way is to use QMutableMapIterator::remove (). In addition, you can clear the entire map using clear ().
QMap'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, QMap's key type must provide operator<(). QMap uses it to keep its items sorted, and assumes that two keys
x
and
y
are equivalent 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.
另請參閱 QMapIterator , QMutableMapIterator , QHash ,和 QSet .
Qt 樣式同義詞 QMap::const_iterator .
Qt 樣式同義詞 QMap::iterator .
The QMap::const_key_value_iterator typedef provides an STL-style iterator for QMap .
QMap::const_key_value_iterator is essentially the same as QMap::const_iterator with the difference that operator*() returns a key/value pair instead of a value.
另請參閱 QKeyValueIterator .
[alias]
QMap::
difference_type
typedef 對於 ptrdiff_t。為兼容 STL 提供。
[alias]
QMap::
key_type
typedef 對於 Key。為兼容 STL 提供。
The QMap::key_value_iterator typedef provides an STL-style iterator for QMap .
QMap::key_value_iterator is essentially the same as QMap::iterator with the difference that operator*() returns a key/value pair instead of a value.
另請參閱 QKeyValueIterator .
[alias]
QMap::
mapped_type
typedef 對於 T。為兼容 STL 提供。
[alias]
QMap::
size_type
typedef 對於 int。為兼容 STL 提供。
[since 6.4]
auto
QMap::
asKeyValueRange
() &
[since 6.4]
auto
QMap::
asKeyValueRange
() &&
[since 6.4]
auto
QMap::
asKeyValueRange
() const &
[since 6.4]
auto
QMap::
asKeyValueRange
() const &&
Returns a range object that allows iteration over this 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:
QMap<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 map. Specifically, mutating the value will modify the map itself.
These functions were introduced in Qt 6.4.
另請參閱 QKeyValueIterator .
Constructs an empty map.
另請參閱 clear ().
[explicit]
QMap::
QMap
(const
std::map
<
Key
,
T
> &
other
)
構造副本為 other .
另請參閱 toStdMap ().
Constructs a map with a copy of each of the elements in the initializer list list .
[explicit]
QMap::
QMap
(
std::map
<
Key
,
T
> &&
other
)
Constructs a map by moving from other .
另請參閱 toStdMap ().
[default]
QMap::
QMap
(const
QMap
<
Key
,
T
> &
other
)
構造副本為 other .
This operation occurs in 常量時間 , because QMap is 隱式共享 . This makes returning a QMap from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takes 綫性時間 .
另請參閱 operator= .
[default]
QMap::
QMap
(
QMap
<
Key
,
T
> &&
other
)
Move-constructs a QMap instance.
[default]
QMap::
~QMap
()
Destroys the map. References to the values in the map, and all iterators over this map, become invalid.
返迴 STL 樣式迭代器 pointing to the first item in the map.
另請參閱 constBegin () 和 end ().
這是重載函數。
返迴常量 STL 樣式迭代器 pointing to the first item in the map.
返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last item in the map.
Removes all items from the map.
另請參閱 remove ().
返迴常量 STL 樣式迭代器 pointing to the first item in the map.
返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last item in the map.
另請參閱 constBegin () 和 end ().
Returns an const iterator pointing to the item with key key in the map.
If the map contains no item with key key ,函數返迴 constEnd ().
另請參閱 find ().
返迴常量 STL 樣式迭代器 pointing to the first entry in the map.
另請參閱 keyValueBegin ().
返迴常量 STL 樣式迭代器 pointing to the imaginary entry after the last entry in the map.
另請參閱 constKeyValueBegin ().
返迴
true
if the map contains an item with key
key
;否則返迴
false
.
另請參閱 count ().
Returns the number of items associated with key key .
另請參閱 contains ().
如同 size ().
這是重載函數。
此函數為兼容 STL (標準模闆庫) 提供。它相當於 isEmpty (), returning true if the map is empty; otherwise returning false.
返迴 STL 樣式迭代器 pointing to the imaginary item after the last item in the map.
這是重載函數。
Returns a pair of iterators delimiting the range of values
[first, second)
, that are stored under
key
.
這是重載函數。
Removes the (key, value) pair pointed to by the iterator pos from the map, and returns an iterator to the next item in the map.
注意: The iterator pos must be valid and dereferenceable.
另請參閱 remove ().
[since 6.0]
QMap
<
Key
,
T
>
::iterator
QMap::
erase
(
QMap
<
Key
,
T
>
::const_iterator
first
,
QMap
<
Key
,
T
>
::const_iterator
last
)
Removes the (key, value) pairs pointed to by the iterator range [ first , last ) from the map. Returns an iterator to the item in the map following the last removed element.
注意:
範圍
[first, last)
must
be a valid range in
*this
.
該函數在 Qt 6.0 引入。
另請參閱 remove ().
Returns an iterator pointing to the item with key key in the map.
If the map contains no item with key key ,函數返迴 end ().
另請參閱 constFind (), value (), 值 (), lowerBound (),和 upperBound ().
這是重載函數。
Returns a reference to the first value in the map, that is the value mapped to the smallest key. This function assumes that the map is not empty.
When unshared (or const version is called), this executes in 常量時間 .
另請參閱 last (), firstKey (),和 isEmpty ().
這是重載函數。
Returns a reference to the smallest key in the map. This function assumes that the map is not empty.
This executes in 常量時間 .
另請參閱 lastKey (), first (), keyBegin (),和 isEmpty ().
Moves all the items from map 進此映射。
If a key is common to both maps, its value will be replaced with the value stored in map .
若 map is shared, then the items will be copied instead.
插入所有項在 map 進此映射。
If a key is common to both maps, its value will be replaced with the value stored in map .
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 .
Returns an iterator pointing to the new/updated element.
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 (strictly) 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 key key , that item's value is replaced with value .
If the hint is correct and the map is unshared, the insert executes in amortized 常量時間 .
When creating a 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 map and the pos map.
Returns an iterator pointing to the new/updated element.
這是重載函數。
返迴
true
若映射不包含項;否則返迴 false。
另請參閱 size ().
返迴第 1 個鍵具有值 value ,或 defaultKey if the map contains no item with value value . If no defaultKey is provided the function returns a default-constructed key .
此函數可能很慢 ( 綫性時間 ),因為 QMap 's internal data structure is optimized for fast lookup by key, not by value.
返迴常量 STL 樣式迭代器 pointing to the first key in the map.
返迴常量 STL 樣式迭代器 pointing to the imaginary item after the last key in the map.
另請參閱 keyBegin () 和 lastKey ().
返迴 STL 樣式迭代器 pointing to the first entry in the map.
另請參閱 keyValueEnd ().
返迴常量 STL 樣式迭代器 pointing to the first entry in the map.
另請參閱 keyValueEnd ().
返迴 STL 樣式迭代器 pointing to the imaginary entry after the last entry in the map.
另請參閱 keyValueBegin ().
返迴常量 STL 樣式迭代器 pointing to the imaginary entry after the last entry in the map.
另請參閱 keyValueBegin ().
Returns a list containing all the keys in the map in ascending order.
The order is guaranteed to be the same as that used by 值 ().
此函數創建新列錶,按 綫性時間 . The time and memory use that entails can be avoided by iterating from keyBegin () 到 keyEnd ().
Returns a list containing all the keys associated with value value in ascending order.
此函數可能很慢 ( 綫性時間 ),因為 QMap 's internal data structure is optimized for fast lookup by key, not by value.
這是重載函數。
Returns a reference to the last value in the 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 ().
這是重載函數。
Returns a reference to the largest key in the map. This function assumes that the map is not empty.
This executes in logarithmic time .
另請參閱 firstKey (), last (), keyEnd (),和 isEmpty ().
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.
另請參閱 upperBound () 和 find ().
這是重載函數。
Removes all the items that have the key key from the map. Returns the number of items removed which will be 1 if the key exists in the map, and 0 otherwise.
[since 6.1]
template <typename Predicate>
QMap
<
Key
,
T
>
::size_type
QMap::
removeIf
(
Predicate
pred
)
Removes all elements for which the predicate pred returns true from the map.
The function supports predicates which take either an argument of type
QMap<Key, T>::iterator
, or an argument of type
std::pair<const Key &, T &>
.
Returns the number of elements removed, if any.
該函數在 Qt 6.1 引入。
Returns the number of (key, value) pairs in the map.
[noexcept]
void
QMap::
swap
(
QMap
<
Key
,
T
> &
other
)
Swaps this map with other 。此操作很快且從不失敗。
Removes the item with the key key from the map and returns the value associated with it.
If the item does not exist in the map, the function simply returns a 默認構造值 .
若不使用返迴值, remove () 效率更高。
另請參閱 remove ().
Returns an STL map equivalent to this QMap .
[since 6.0]
std::map
<
Key
,
T
> QMap::
toStdMap
() &&
注意: Calling this function will leave this QMap in the partially-formed state, in which the only valid operations are destruction or assignment of a new value.
這是重載函數。
該函數在 Qt 6.0 引入。
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.
範例:
QMap<int, QString> map; map.insert(1, "one"); map.insert(5, "five"); map.insert(10, "ten"); map.upperBound(0); // returns iterator to (1, "one") map.upperBound(1); // returns iterator to (5, "five") map.upperBound(2); // returns iterator to (5, "five") map.upperBound(10); // returns end() map.upperBound(999); // returns end()
另請參閱 lowerBound () 和 find ().
這是重載函數。
Returns the value associated with the key key .
If the map contains no item with key key ,函數返迴 defaultValue . If no defaultValue is specified, the function returns a 默認構造值 .
另請參閱 key (), 值 (), contains (),和 operator[] ().
Returns a list containing all the values in the map, in ascending order of their keys.
此函數創建新列錶,按 綫性時間 . The time and memory use that entails can be avoided by iterating from keyValueBegin () 到 keyValueEnd ().
[default]
QMap
<
Key
,
T
> &QMap::
operator=
(
QMap
<
Key
,
T
> &&
other
)
移動賦值 other 到此 QMap 實例。
[default]
QMap
<
Key
,
T
> &QMap::
operator=
(const
QMap
<
Key
,
T
> &
other
)
賦值 other to this map and returns a reference to this map.
Returns the value associated with the key key 作為可修改引用。
If the map contains no item with key key , the function inserts a 默認構造值 into the map with key key , and returns a reference to it.
如同 value ().
這是重載函數。
[since 6.1]
template <typename Key, typename T, typename Predicate>
qsizetype
erase_if
(
QMap
<
Key
,
T
> &
map
,
Predicate
pred
)
Removes all elements for which the predicate pred returns true from the map map .
The function supports predicates which take either an argument of type
QMap<Key, T>::iterator
, or an argument of type
std::pair<const Key &, T &>
.
Returns the number of elements removed, if any.
該函數在 Qt 6.1 引入。
[since 6.8]
size_t
qHash
(const
QMap
<
Key
,
T
> &
key
,
size_t
seed
= 0)
返迴哈希值為 key ,使用 seed 做計算種子。
類型
Key
and
T
must be supported by qHash().
該函數在 Qt 6.8 引入。
返迴
true
if
lhs
不等於
rhs
;否則返迴 false。
Two maps are considered equal if they contain the same (key, value) pairs.
This function requires the key and the value types to implement
operator==()
.
另請參閱 operator== ().
寫入映射 map 到流 out .
This function requires the key and value types to implement
operator<<()
.
另請參閱 QDataStream 運算符格式 .
返迴
true
if
lhs
等於
rhs
;否則返迴 false。
Two maps are considered equal if they contain the same (key, value) pairs.
This function requires the key and the value types to implement
operator==()
.
另請參閱 operator!= ().
讀取映射從流 in into map .
This function requires the key and value types to implement
operator>>()
.
另請參閱 QDataStream 運算符格式 .