QMultiHash Class | Qt Core (original) (raw)

This class is equality-comparable.

Member Function Documentation

template <typename... Args> QMultiHash<Key, T>::iterator QMultiHash::emplace(Key &&key, Args &&... args)

template <typename... Args> QMultiHash<Key, T>::iterator QMultiHash::emplace(const Key &key, Args &&... args)

Inserts a new element into the container. This new element is constructed in-place using args as the arguments for its construction.

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

Returns an iterator pointing to the new element.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also insert.

template <typename... Args> QMultiHash<Key, T>::iterator QMultiHash::emplaceReplace(Key &&key, Args &&... args)

template <typename... Args> QMultiHash<Key, T>::iterator QMultiHash::emplaceReplace(const Key &key, Args &&... args)

Inserts a new element into the container. This new element is constructed in-place using args as the arguments for its construction.

If there is already an item with the same key in the hash, that item's value is replaced with a value constructed from args.

Returns an iterator pointing to the new element.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also replace and emplace.

[noexcept] T QMultiHash::value(const Key &key) const

[noexcept] T QMultiHash::value(const Key &key, const T &defaultValue) const

Returns the value associated with the key.

If the hash contains no item with the key, the function returns defaultValue, or a default-constructed value if this parameter has not been supplied.

If there are multiple items for the key in the hash, the value of the most recently inserted one is returned.

[noexcept] Key QMultiHash::key(const T &value) const

[noexcept] Key QMultiHash::key(const T &value, const Key &defaultKey) const

Returns the first key mapped to value. If the hash contains no item mapped to value, returns defaultKey, or a default-constructed key if this parameter has not been supplied.

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

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

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

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

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

Returns a range object that allows iteration over this hash 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:

QMultiHash<QString, int> hash; hash.insert("January", 1); hash.insert("February", 2); // ... hash.insert("December", 12);

for (auto [key, value] : hash.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 hash. Specifically, mutating the value will modify the hash itself.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

This function was introduced in Qt 6.4.

See also QKeyValueIterator.

[noexcept] QMultiHash::QMultiHash()

Constructs an empty hash.

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

Constructs a copy of other (which can be a QHash or a QMultiHash).

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.

template 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 first and second data members (like std::pair), 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 respectively.

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

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also constBegin() and end().

[noexcept] QMultiHash<Key, T>::const_iterator QMultiHash::begin() const

This is an overloaded function.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

[noexcept] QMultiHash<Key, T>::const_iterator QMultiHash::cbegin() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also begin() and cend().

[noexcept] QMultiHash<Key, T>::const_iterator QMultiHash::cend() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also cbegin() and end().

[noexcept(...)] void QMultiHash::clear()

Removes all items from the hash and frees up all memory used by it.

Note: This function is noexcept when std::is_nothrow_destructible<Node>::value is true.

See also remove().

[noexcept] QMultiHash<Key, T>::const_iterator QMultiHash::constBegin() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also begin() and constEnd().

[noexcept] QMultiHash<Key, T>::const_iterator QMultiHash::constEnd() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also constBegin() and end().

[noexcept] QMultiHash<Key, T>::const_iterator QMultiHash::constFind(const Key &key, const T &value) const

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

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

[noexcept] QMultiHash<Key, T>::const_key_value_iterator QMultiHash::constKeyValueBegin() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also keyValueBegin().

[noexcept] QMultiHash<Key, T>::const_key_value_iterator QMultiHash::constKeyValueEnd() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also constKeyValueBegin().

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

Returns true if the hash contains an item with the key and value; otherwise returns false.

See also contains().

[noexcept] qsizetype QMultiHash::count(const Key &key, const T &value) const

Returns the number of items with the key and value.

See also count().

[noexcept] QMultiHash<Key, T>::iterator QMultiHash::end()

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also begin() and constEnd().

[noexcept] QMultiHash<Key, T>::const_iterator QMultiHash::end() const

This is an overloaded function.

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

Returns a pair of iterators delimiting the range of values [first, second), that are stored under key. If the range is empty then both iterators will be equal to end().

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

[noexcept] std::pair<QMultiHash<Key, T>::const_iterator, QMultiHash<Key, T>::const_iterator> QMultiHash::equal_range(const Key &key) const

This is an overloaded function.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

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

Returns an iterator pointing to the item with the 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.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

[noexcept] QMultiHash<Key, T>::const_iterator QMultiHash::find(const Key &key, const T &value) const

This is an overloaded function.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

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

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

Returns an iterator pointing to the new element.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also replace().

[noexcept] QMultiHash<Key, T>::key_iterator QMultiHash::keyBegin() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also keyEnd().

[noexcept] QMultiHash<Key, T>::key_iterator QMultiHash::keyEnd() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also keyBegin().

[noexcept] QMultiHash<Key, T>::key_value_iterator QMultiHash::keyValueBegin()

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also keyValueEnd().

[noexcept] QMultiHash<Key, T>::const_key_value_iterator QMultiHash::keyValueBegin() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also keyValueEnd().

[noexcept] QMultiHash<Key, T>::key_value_iterator QMultiHash::keyValueEnd()

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also keyValueBegin().

[noexcept] QMultiHash<Key, T>::const_key_value_iterator QMultiHash::keyValueEnd() const

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

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also keyValueBegin().

QList<Key> QMultiHash::keys() const

Returns a list containing all the keys in the hash, in an arbitrary order. Keys that occur multiple times in the hash 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 linear time. The time and memory use that entails can be avoided by iterating from keyBegin() to keyEnd().

See also values() and key().

qsizetype QMultiHash::remove(const Key &key)

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

See also remove().

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

See also remove().

[since 6.1] template qsizetype QMultiHash::removeIf(Predicate pred)

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

The function supports predicates which take either an argument of type QMultiHash<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().

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

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

Returns an iterator pointing to the new/updated element.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also insert().

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

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

T QMultiHash::take(const Key &key)

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

If the item does not exist in the hash, the function simply returns a default-constructed value. If there are multiple items for key in the hash, only the most recently inserted one is removed.

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

See also remove().

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.

See also keys() and values().

[since 6.0] QMultiHash<Key, T> &QMultiHash::unite(const QHash<Key, T> &other)

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

This function was introduced in Qt 6.0.

See also insert().

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

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

See also insert().

QList<T> QMultiHash::values() const

Returns a list containing all the values in the hash, in an arbitrary order. 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.

The order is guaranteed to be the same as that used by 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().

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

This is an overloaded function.

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

See also count() and insert().

QMultiHash<Key, T> QMultiHash::operator+(const QMultiHash<Key, T> &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.

See also operator+=().

QMultiHash<Key, T> &QMultiHash::operator+=(const QMultiHash<Key, T> &other)

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

See also unite() and insert().

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

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

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

If the hash contains multiple items with the key, this function returns a reference to the most recently inserted value.

Warning: Returned iterators/references should be considered invalidated the next time you call a non-const function on the hash, or when the hash is destroyed.

See also insert() and value().