QMap Class | Qt Core (original) (raw)

This class is equality-comparable.

Member Function Documentation

[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.

This function was introduced in Qt 6.4.

See also QKeyValueIterator.

QMap::QMap()

Constructs an empty map.

See also clear().

[explicit] QMap::QMap(const std::map<Key, T> &other)

Constructs a copy of other.

See also toStdMap().

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

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.

See also toStdMap().

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

Constructs a copy of other.

This operation occurs in constant time, because QMap is implicitly shared. 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 linear time.

See also 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.

QMap<Key, T>::iterator QMap::begin()

Returns an STL-style iterator pointing to the first item in the map.

See also constBegin() and end().

QMap<Key, T>::const_iterator QMap::begin() const

This is an overloaded function.

QMap<Key, T>::const_iterator QMap::cbegin() const

Returns a const STL-style iterator pointing to the first item in the map.

See also begin() and cend().

QMap<Key, T>::const_iterator QMap::cend() const

Returns a const STL-style iterator pointing to the imaginary item after the last item in the map.

See also cbegin() and end().

void QMap::clear()

Removes all items from the map.

See also remove().

QMap<Key, T>::const_iterator QMap::constBegin() const

Returns a const STL-style iterator pointing to the first item in the map.

See also begin() and constEnd().

QMap<Key, T>::const_iterator QMap::constEnd() const

Returns a const STL-style iterator pointing to the imaginary item after the last item in the map.

See also constBegin() and end().

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

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

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

See also find().

QMap<Key, T>::const_key_value_iterator QMap::constKeyValueBegin() const

Returns a const STL-style iterator pointing to the first entry in the map.

See also keyValueBegin().

QMap<Key, T>::const_key_value_iterator QMap::constKeyValueEnd() const

Returns a const STL-style iterator pointing to the imaginary entry after the last entry in the map.

See also constKeyValueBegin().

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

Returns true if the map contains an item with key key; otherwise returns false.

See also count().

QMap<Key, T>::size_type QMap::count(const Key &key) const

Returns the number of items associated with key key.

See also contains().

QMap<Key, T>::size_type QMap::count() const

This is an overloaded function.

Same as size().

bool QMap::empty() const

This function is provided for STL compatibility. It is equivalent to isEmpty(), returning true if the map is empty; otherwise returning false.

QMap<Key, T>::iterator QMap::end()

Returns an STL-style iterator pointing to the imaginary item after the last item in the map.

See also begin() and constEnd().

QMap<Key, T>::const_iterator QMap::end() const

This is an overloaded function.

std::pair<QMap<Key, T>::iterator, QMap<Key, T>::iterator> QMap::equal_range(const Key &key)

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

std::pair<QMap<Key, T>::const_iterator, QMap<Key, T>::const_iterator> QMap::equal_range(const Key &key) const

This is an overloaded function.

QMap<Key, T>::iterator QMap::erase(QMap<Key, T>::const_iterator pos)

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.

Note: The iterator pos must be valid and dereferenceable.

See also 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.

Note: The range [first, last) must be a valid range in *this.

This function was introduced in Qt 6.0.

See also remove().

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

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

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

See also constFind(), value(), values(), lowerBound(), and upperBound().

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

This is an overloaded function.

T &QMap::first()

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 constant time.

See also last(), firstKey(), and isEmpty().

const T &QMap::first() const

This is an overloaded function.

const Key &QMap::firstKey() const

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

This executes in constant time.

See also lastKey(), first(), keyBegin(), and isEmpty().

void QMap::insert(QMap<Key, T> &&map)

Moves all the items from map into this map.

If a key is common to both maps, its value will be replaced with the value stored in map.

If map is shared, then the items will be copied instead.

void QMap::insert(const QMap<Key, T> &map)

Inserts all the items in map into this map.

If a key is common to both maps, its value will be replaced with the value stored in map.

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

Inserts a new item with the key key and a value of 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.

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

This is an overloaded function.

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

If 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 constant time.

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.

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

bool QMap::isEmpty() const

Returns true if the map contains no items; otherwise returns false.

See also size().

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

This is an overloaded function.

Returns the first key with value value, or defaultKey if the map contains no item with value value. If no defaultKey is provided the function returns a default-constructed key.

This function can be slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.

See also value() and keys().

QMap<Key, T>::key_iterator QMap::keyBegin() const

Returns a const STL-style iterator pointing to the first key in the map.

See also keyEnd() and firstKey().

QMap<Key, T>::key_iterator QMap::keyEnd() const

Returns a const STL-style iterator pointing to the imaginary item after the last key in the map.

See also keyBegin() and lastKey().

QMap<Key, T>::key_value_iterator QMap::keyValueBegin()

Returns an STL-style iterator pointing to the first entry in the map.

See also keyValueEnd().

QMap<Key, T>::const_key_value_iterator QMap::keyValueBegin() const

Returns a const STL-style iterator pointing to the first entry in the map.

See also keyValueEnd().

QMap<Key, T>::key_value_iterator QMap::keyValueEnd()

Returns an STL-style iterator pointing to the imaginary entry after the last entry in the map.

See also keyValueBegin().

QMap<Key, T>::const_key_value_iterator QMap::keyValueEnd() const

Returns a const STL-style iterator pointing to the imaginary entry after the last entry in the map.

See also keyValueBegin().

QList<Key> QMap::keys() const

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 values().

This function creates a new list, in linear time. The time and memory use that entails can be avoided by iterating from keyBegin() to keyEnd().

See also values() and key().

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

This is an overloaded function.

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

This function can be slow (linear time), because QMap's internal data structure is optimized for fast lookup by key, not by value.

T &QMap::last()

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.

See also first(), lastKey(), and isEmpty().

const T &QMap::last() const

This is an overloaded function.

const Key &QMap::lastKey() const

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

This executes in logarithmic time.

See also firstKey(), last(), keyEnd(), and isEmpty().

QMap<Key, T>::iterator QMap::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.

See also upperBound() and find().

QMap<Key, T>::const_iterator QMap::lowerBound(const Key &key) const

This is an overloaded function.

QMap<Key, T>::size_type QMap::remove(const Key &key)

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.

See also clear() and take().

[since 6.1] template 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.

This function was introduced in Qt 6.1.

See also clear() and take().

QMap<Key, T>::size_type QMap::size() const

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

See also isEmpty() and count().

[noexcept] void QMap::swap(QMap<Key, T> &other)

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

T QMap::take(const Key &key)

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 default-constructed value.

If you don't use the return value, remove() is more efficient.

See also remove().

std::map<Key, T> QMap::toStdMap() const &

Returns an STL map equivalent to this QMap.

[since 6.0] std::map<Key, T> QMap::toStdMap() &&

This is an overloaded function.

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

This function was introduced in Qt 6.0.

QMap<Key, T>::iterator QMap::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.

Example:

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()

See also lowerBound() and find().

QMap<Key, T>::const_iterator QMap::upperBound(const Key &key) const

This is an overloaded function.

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

Returns the value associated with the key key.

If the map contains no item with key key, the function returns defaultValue. If no defaultValue is specified, the function returns a default-constructed value.

See also key(), values(), contains(), and operator[]().

QList<T> QMap::values() const

Returns a list containing all the values in the map, in ascending order of their keys.

This function creates a new list, in linear time. The time and memory use that entails can be avoided by iterating from keyValueBegin() to keyValueEnd().

See also keys() and value().

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

Move-assigns other to this QMap instance.

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

Assigns other to this map and returns a reference to this map.

T &QMap::operator[](const Key &_key_)

Returns the value associated with the key key as a modifiable reference.

If the map contains no item with key key, the function inserts a default-constructed value into the map with key key, and returns a reference to it.

See also insert() and value().

T QMap::operator[](const Key &_key_) const

This is an overloaded function.

Same as value().