QHash Class | Qt Core | Qt 6.11.2
Description: The QHash class is a template class that provides a hash-table-based dictionary.

- Qt 6.11
- Qt Core
- C++ Classes
- QHash
- Qt 6.11.2 Reference Documentation

# QHash Class

template <typename Key, typename T> class QHash

The QHash class is a template class that provides a hash-table-based dictionary. More...

Header: | `#include <QHash>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`

- List of all members, including inherited members
- Deprecated members
- QHash is part of Implicitly Shared Classes.

This class is equality-comparable.

Note: All functions in this class are reentrant.

## Public Types

`(since 6.9)` struct | TryEmplaceResult
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

## Public Functions

| QHash()
| QHash(std::initializer_list<std::pair<Key, T>> list)
| QHash(InputIterator begin, InputIterator end)
| QHash(const QHash<Key, T> &other)
| QHash(QHash<Key, T> &&other)
| ~QHash()
`(since 6.4)` auto | asKeyValueRange() &&
`(since 6.4)` auto | asKeyValueRange() &
`(since 6.4)` auto | asKeyValueRange() const &&
`(since 6.4)` auto | asKeyValueRange() const &
QHash<Key, T>::iterator | begin()
QHash<Key, T>::const_iterator | begin() const
qsizetype | capacity() const
QHash<Key, T>::const_iterator | cbegin() const
QHash<Key, T>::const_iterator | cend() const
void | clear()
QHash<Key, T>::const_iterator | constBegin() const
QHash<Key, T>::const_iterator | constEnd() const
QHash<Key, T>::const_iterator | constFind(const Key &key) const
QHash<Key, T>::const_key_value_iterator | constKeyValueBegin() const
QHash<Key, T>::const_key_value_iterator | constKeyValueEnd() const
bool | contains(const Key &key) const
qsizetype | count(const Key &key) const
qsizetype | count() const
QHash<Key, T>::iterator | emplace(Key &&key, Args &&... args)
QHash<Key, T>::iterator | emplace(const Key &key, Args &&... args)
bool | empty() const
QHash<Key, T>::iterator | end()
QHash<Key, T>::const_iterator | end() const
QHash<Key, T>::iterator | erase(QHash<Key, T>::const_iterator pos)
QHash<Key, T>::iterator | find(const Key &key)
QHash<Key, T>::const_iterator | find(const Key &key) const
void | insert(const QHash<Key, T> &other)
QHash<Key, T>::iterator | insert(const Key &key, const T &value)
`(since 6.11)` QHash<Key, T>::iterator | insert(Key &&key, T &&value)
`(since 6.11)` QHash<Key, T>::iterator | insert(Key &&key, const T &value)
`(since 6.11)` QHash<Key, T>::iterator | insert(const Key &key, T &&value)
`(since 6.9)` QHash<Key, T>::TryEmplaceResult | insertOrAssign(K &&key, Value &&value)
`(since 6.9)` QHash<Key, T>::TryEmplaceResult | insertOrAssign(Key &&key, Value &&value)
`(since 6.9)` QHash<Key, T>::TryEmplaceResult | insertOrAssign(const Key &key, Value &&value)
`(since 6.9)` std::pair<QHash<Key, T>::key_value_iterator, bool> | insert_or_assign(K &&key, Value &&value)
`(since 6.9)` std::pair<QHash<Key, T>::key_value_iterator, bool> | insert_or_assign(Key &&key, Value &&value)
`(since 6.9)` std::pair<QHash<Key, T>::key_value_iterator, bool> | insert_or_assign(const Key &key, Value &&value)
`(since 6.9)` QHash<Key, T>::key_value_iterator | insert_or_assign(QHash<Key, T>::const_iterator hint, K &&key, Value &&value)
`(since 6.9)` QHash<Key, T>::key_value_iterator | insert_or_assign(QHash<Key, T>::const_iterator hint, Key &&key, Value &&value)
`(since 6.9)` QHash<Key, T>::key_value_iterator | insert_or_assign(QHash<Key, T>::const_iterator hint, const Key &key, Value &&value)
bool | isEmpty() const
Key | key(const T &value) const
Key | key(const T &value, const Key &defaultKey) const
QHash<Key, T>::key_iterator | keyBegin() const
QHash<Key, T>::key_iterator | keyEnd() const
QHash<Key, T>::key_value_iterator | keyValueBegin()
QHash<Key, T>::const_key_value_iterator | keyValueBegin() const
QHash<Key, T>::key_value_iterator | keyValueEnd()
QHash<Key, T>::const_key_value_iterator | keyValueEnd() const
QList<Key> | keys() const
QList<Key> | keys(const T &value) const
float | load_factor() const
bool | remove(const Key &key)
`(since 6.1)` qsizetype | removeIf(Predicate pred)
void | reserve(qsizetype size)
qsizetype | size() const
void | squeeze()
void | swap(QHash<Key, T> &other)
T | take(const Key &key)
`(since 6.9)` QHash<Key, T>::TryEmplaceResult | tryEmplace(K &&key, Args &&... args)
`(since 6.9)` QHash<Key, T>::TryEmplaceResult | tryEmplace(Key &&key, Args &&... args)
`(since 6.9)` QHash<Key, T>::TryEmplaceResult | tryEmplace(const Key &key, Args &&... args)
`(since 6.9)` QHash<Key, T>::TryEmplaceResult | tryInsert(K &&key, const T &value)
`(since 6.9)` QHash<Key, T>::TryEmplaceResult | tryInsert(const Key &key, const T &value)
`(since 6.9)` std::pair<QHash<Key, T>::key_value_iterator, bool> | try_emplace(K &&key, Args &&... args)
`(since 6.9)` std::pair<QHash<Key, T>::key_value_iterator, bool> | try_emplace(Key &&key, Args &&... args)
`(since 6.9)` std::pair<QHash<Key, T>::key_value_iterator, bool> | try_emplace(const Key &key, Args &&... args)
`(since 6.9)` QHash<Key, T>::key_value_iterator | try_emplace(QHash<Key, T>::const_iterator hint, K &&key, Args &&... args)
`(since 6.9)` QHash<Key, T>::key_value_iterator | try_emplace(QHash<Key, T>::const_iterator hint, Key &&key, Args &&... args)
`(since 6.9)` QHash<Key, T>::key_value_iterator | try_emplace(QHash<Key, T>::const_iterator hint, const Key &key, Args &&... args)
T | value(const Key &key) const
T | value(const Key &key, const T &defaultValue) const
QList<T> | values() const
QHash<Key, T> & | operator=(QHash<Key, T> &&other)
QHash<Key, T> & | operator=(const QHash<Key, T> &other)
T & | operator[](const Key &key)
const T | operator[](const Key &key) const

## Related Non-Members

`(since 6.1)` qsizetype | erase_if(QHash<Key, T> &hash, Predicate pred)
`(since 6.5)` size_t | qHash(Enum key, size_t seed = 0)
size_t | qHash(const QStringRef &key, size_t seed = 0)
size_t | qHash(const QMqttTopicFilter &filter, size_t seed = 0)
size_t | qHash(QSslEllipticCurve key, size_t seed = 0)
size_t | qHash(const QGeoCoordinate &coordinate, size_t seed = 0)
`(since 6.0)` size_t | qHash(QByteArrayView key, size_t seed = 0)
size_t | qHash(const QMqttTopicName &name, size_t seed = 0)
size_t | qHash(const QOcspResponse &key, size_t seed = 0)
size_t | qHash(uint key, size_t seed = 0)
size_t | qHash(ulong key, size_t seed = 0)
size_t | qHash(ushort key, size_t seed = 0)
`(since 6.0)` size_t | qHash(wchar_t key, size_t seed = 0)
size_t | qHash(QDate key, size_t seed = 0)
size_t | qHash(const QSslCertificate &key, size_t seed = 0)
size_t | qHash(QLatin1StringView key, size_t seed = 0)
size_t | qHash(const QSslError &key, size_t seed = 0)
`(since 6.0)` size_t | qHash(QPoint key, size_t seed = 0)
size_t | qHash(QTime key, size_t seed = 0)
`(since 6.9)` size_t | qHash(T key, size_t seed)
size_t | qHash(char key, size_t seed = 0)
`(since 6.0)` size_t | qHash(char16_t key, size_t seed = 0)
`(since 6.0)` size_t | qHash(char32_t key, size_t seed = 0)
`(since 6.0)` size_t | qHash(char8_t key, size_t seed = 0)
size_t | qHash(const QBitArray &key, size_t seed = 0)
size_t | qHash(const QByteArray &key, size_t seed = 0)
size_t | qHash(const QChar key, size_t seed = 0)
size_t | qHash(const QDateTime &key, size_t seed = 0)
size_t | qHash(const QHash<Key, T> &key, size_t seed = 0)
size_t | qHash(const QSet<T> &key, size_t seed = 0)
size_t | qHash(const QString &key, size_t seed = 0)
`(since 6.0)` size_t | qHash(const QTypeRevision &key, size_t seed = 0)
size_t | qHash(const QUrl &key, size_t seed = 0)
size_t | qHash(const QVersionNumber &key, size_t seed = 0)
size_t | qHash(const T *key, size_t seed = 0)
size_t | qHash(const std::pair<T1, T2> &key, size_t seed = 0)
size_t | qHash(double key, size_t seed = 0)
size_t | qHash(float key, size_t seed = 0)
size_t | qHash(int key, size_t seed = 0)
size_t | qHash(long key, size_t seed = 0)
size_t | qHash(long double key, size_t seed = 0)
`(since 6.8)` size_t | qHash(qint128 key, size_t seed = 0)
size_t | qHash(qint64 key, size_t seed = 0)
`(since 6.8)` size_t | qHash(quint128 key, size_t seed = 0)
size_t | qHash(quint64 key, size_t seed = 0)
size_t | qHash(short key, size_t seed = 0)
size_t | qHash(signed char key, size_t seed = 0)
`(since 6.0)` size_t | qHash(std::nullptr_t key, size_t seed = 0)
size_t | qHash(uchar key, size_t seed = 0)
size_t | qHashBits(const void *p, size_t len, size_t seed = 0)
`(since 6.0)` size_t | qHashMulti(size_t seed, const T &... args)
`(since 6.0)` size_t | qHashMultiCommutative(size_t seed, const T &... args)
size_t | qHashRange(InputIterator first, InputIterator last, size_t seed = 0)
size_t | qHashRangeCommutative(InputIterator first, InputIterator last, size_t seed = 0)
bool | operator!=(const QHash<Key, T> &lhs, const QHash<Key, T> &rhs)
QDataStream & | operator<<(QDataStream &out, const QHash<Key, T> &hash)
bool | operator==(const QHash<Key, T> &lhs, const QHash<Key, T> &rhs)
QDataStream & | operator>>(QDataStream &in, QHash<Key, T> &hash)

## Macros

`(since 6.11)` | QT_NO_SINGLE_ARGUMENT_QHASH_OVERLOAD

## Detailed Description

QHash<Key, T> is one of Qt's generic container classes. It stores (key, value) pairs and provides very fast lookup of the value associated with a key.

QHash provides very similar functionality to QMap. The differences are:

- QHash provides faster lookups than QMap. (See Algorithmic Complexity for details.)
- When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.
- The key type of a QMap must provide operator<(). The key type of a QHash must provide operator==() and a global hash function called qHash() (see qHash).

Here's an example QHash with QString keys and `int` values:

     QHash<QString, int> hash;

To insert a (key, value) pair into the hash, you can use operator[]():

     hash["one"] = 1;
     hash["three"] = 3;
     hash["seven"] = 7;

This inserts the following three (key, value) pairs into the QHash: ("one", 1), ("three", 3), and ("seven", 7). Another way to insert items into the hash is to use insert():

     hash.insert("twelve", 12);

To look up a value, use operator[]() or value():

     int num1 = hash["thirteen"];
     int num2 = hash.value("thirteen");

If there is no item with the specified key in the hash, these functions return a default-constructed value.

If you want to check whether the hash contains a particular key, use contains():

     int timeout = 30;
     if (hash.contains("TIMEOUT"))
         timeout = hash.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 = hash.value("TIMEOUT", 30);

In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is that operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code snippet will create 1000 items in memory:

     // WRONG
     QHash<int, QWidget *> hash;
     //...
     for (int i = 0; i < 1000; ++i) {
         if (hash[i] == okButton)
             cout << "Found button at index " << i << endl;
     }

To avoid this problem, replace `hash[i]` with `hash.value(i)` in the code above.

Internally, QHash uses a hash table to perform lookups. This hash table automatically grows to provide fast lookups without wasting too much memory. You can still control the size of the hash table by calling reserve() if you already know approximately how many items the QHash will contain, but this isn't necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.

QHash will not shrink automatically if items are removed from the table. To minimize the memory used by the hash, call squeeze().

To iterate through all the (key, value) pairs stored in a QHash, use asKeyValueRange():

     for (const auto &[key, value] : std::as_const(hash).asKeyValueRange())
         cout << qPrintable(key) << ": " << value << endl;

This function returns a range object that can be used with structured bindings. For manual iterator control, you can also use traditional STL-style iterators (QHash::const_iterator and QHash::iterator):

     for (auto it = hash.cbegin(); it != hash.cend(); ++it)
         cout << qPrintable(it.key()) << ": " << it.value() << endl;

To modify values, use iterators:

     for (auto it = hash.begin(); it != hash.end(); ++it)
         it.value() += 1;

QHash also provides Java-style iterators (QHashIterator and QMutableHashIterator) for compatibility.

QHash is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap.

A QHash allows only one value per key. If you call insert() with a key that already exists in the QHash, the previous value is erased. For example:

     hash.insert("plenty", 100);
     hash.insert("plenty", 2000);
     // hash.value("plenty") == 2000

If you need to store multiple entries for the same key in the hash table, use QMultiHash.

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

     QHash<QString, int> hash;
     //...
     for (int value : std::as_const(hash))
         cout << value << endl;

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

QHash's key and value data types must be assignable data types. You cannot, for example, store a QWidget as a value; instead, store a QWidget *.

#### The hashing function

A QHash's key type has additional requirements other than being an assignable data type: it must provide operator==(), and there must also be a hashing function that returns a hash value for an argument of the key's type.

The hashing function computes a numeric value based on a key. It can use any algorithm imaginable, as long as it always returns the same value if given the same argument. In other words, if `e1 == e2`, then `hash(e1) == hash(e2)` must hold as well. However, to obtain good performance, the hashing function should attempt to return different hash values for different keys to the largest extent possible.

A hashing function for a key type `K` may be provided in two different ways.

The first way is by having an overload of `qHash()` in `K`'s namespace. The `qHash()` function must have one of these signatures:

     size_t qHash(K key, size_t seed);
     size_t qHash(const K &key, size_t seed);

     size_t qHash(K key);        // deprecated, do not use
     size_t qHash(const K &key); // deprecated, do not use

The two-arguments overloads take an unsigned integer that should be used to seed the calculation of the hash function. This seed is provided by QHash in order to prevent a family of algorithmic complexity attacks.

Note: In Qt 6 it is possible to define a `qHash()` overload taking only one argument; support for this is deprecated. Starting with Qt 7, it will be mandatory to use a two-arguments overload. If both a one-argument and a two-arguments overload are defined for a key type, the latter is used by QHash (note that you can simply define a two-arguments version, and use a default value for the seed parameter). In Qt 6 it is possible to disable support for the single argument qHash overload by defining the `QT_NO_SINGLE_ARGUMENT_QHASH_OVERLOAD` macro.

The second way to provide a hashing function is by specializing the `std::hash` class for the key type `K`, and providing a suitable function call operator for it:

     namespace std
     {
         template <> struct hash<K>
         {
             // seed is optional
             size_t operator()(const K &key, size_t seed = 0) const;
         };
     }

The seed argument has the same meaning as for `qHash()`, and may be left out.

This second way allows to reuse the same hash function between QHash and the C++ Standard Library unordered associative containers. If both a `qHash()` overload and a `std::hash` specializations are provided for a type, then the `qHash()` overload is preferred.

Here's a partial list of the C++ and Qt types that can serve as keys in a QHash: any integer type (char, unsigned long, etc.), any pointer type, QChar, QString, and QByteArray. For all of these, the `<QHash>` header defines a qHash() function that computes an adequate hash value. Many other Qt classes also declare a qHash overload for their type; please refer to the documentation of each class.

If you want to use other types as the key, make sure that you provide operator==() and a hash implementation.

The convenience qHashMulti() function can be used to implement qHash() for a custom type, where one usually wants to produce a hash value from multiple fields:

Example:

     #ifndef EMPLOYEE_H
     #define EMPLOYEE_H

     class Employee
     {
     public:
         Employee() {}
         Employee(const QString &name, QDate dateOfBirth);
         QString name() const { return myName; }
         QDate dateOfBirth() const { return myDateOfBirth; }
         //...

     private:
         QString myName;
         QDate myDateOfBirth;
     };

     inline bool operator==(const Employee &e1, const Employee &e2)
     {
         return e1.name() == e2.name()
                && e1.dateOfBirth() == e2.dateOfBirth();
     }

     inline size_t qHash(const Employee &key, size_t seed)
     {
         return qHashMulti(seed, key.name(), key.dateOfBirth());
     }

     #endif // EMPLOYEE_H

In the example above, we've relied on Qt's own implementation of qHash() for QString and QDate to give us a hash value for the employee's name and date of birth respectively.

Note that the implementation of the qHash() overloads offered by Qt may change at any time. You must not rely on the fact that qHash() will give the same results (for the same inputs) across different Qt versions.

#### Algorithmic complexity attacks

All hash tables are vulnerable to a particular class of denial of service attacks, in which the attacker carefully pre-computes a set of different keys that are going to be hashed in the same bucket of a hash table (or even have the very same hash value). The attack aims at getting the worst-case algorithmic behavior (O(n) instead of amortized O(1), see Algorithmic Complexity for the details) when the data is fed into the table.

In order to avoid this worst-case behavior, the calculation of the hash value done by qHash() can be salted by a random seed, that nullifies the attack's extent. This seed is automatically generated by QHash once per process, and then passed by QHash as the second argument of the two-arguments overload of the qHash() function.

This randomization of QHash is enabled by default. Even though programs should never depend on a particular QHash ordering, there may be situations where you temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization, define the environment variable `QT_HASH_SEED` to have the value 0. Alternatively, you can call the QHashSeed::setDeterministicGlobalSeed() function.

See also QHashIterator, QMutableHashIterator, QMap, and QSet.

## Member Type Documentation

### QHash::ConstIterator

Qt-style synonym for QHash::const_iterator.

### QHash::Iterator

Qt-style synonym for QHash::iterator.

### QHash::const_key_value_iterator

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

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

See also QKeyValueIterator.

### `[alias]` QHash::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

### `[alias]` QHash::key_type

Typedef for Key. Provided for STL compatibility.

### QHash::key_value_iterator

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

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

See also QKeyValueIterator.

### `[alias]` QHash::mapped_type

Typedef for T. Provided for STL compatibility.

### `[alias]` QHash::size_type

Typedef for int. Provided for STL compatibility.

## Member Function Documentation

### `[noexcept]` QHash::QHash()

Constructs an empty hash.

See also clear().

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

Constructs a hash with a copy of each of the elements in the initializer list list.

### template <typename InputIterator> QHash::QHash(InputIterator begin, InputIterator end)

Constructs a 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.

### `[noexcept]` QHash::QHash(const QHash<Key, T> &other)

Constructs a copy of other.

This operation occurs in constant time, because QHash is implicitly shared. This makes returning a QHash 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=().

### `[noexcept]` QHash::QHash(QHash<Key, T> &&other)

Move-constructs a QHash instance, making it point at the same object that other was pointing to.

### QHash::~QHash()

Destroys the hash. References to the values in the hash and all iterators of this hash become invalid.

### `[since 6.4]` auto QHash::asKeyValueRange() &

### `[since 6.4]` auto QHash::asKeyValueRange() const &

### `[since 6.4]` auto QHash::asKeyValueRange() &&

### `[since 6.4]` auto QHash::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:

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

These functions were introduced in Qt 6.4.

See also QKeyValueIterator.

### QHash<Key, T>::iterator QHash::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]` QHash<Key, T>::const_iterator QHash::begin() const

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 is an overloaded function.

### `[noexcept]` qsizetype QHash::capacity() const

Returns the number of buckets in the QHash's internal hash table.

The sole purpose of this function is to provide a means of fine tuning QHash's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the hash, call size().

See also reserve() and squeeze().

### `[noexcept]` QHash<Key, T>::const_iterator QHash::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]` QHash<Key, T>::const_iterator QHash::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 QHash::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]` QHash<Key, T>::const_iterator QHash::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]` QHash<Key, T>::const_iterator QHash::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]` QHash<Key, T>::const_iterator QHash::constFind(const Key &key) const

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

If the hash contains no item with the key, 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.

See also find().

### `[noexcept]` QHash<Key, T>::const_key_value_iterator QHash::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]` QHash<Key, T>::const_key_value_iterator QHash::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 QHash::contains(const Key &key) const

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

See also count().

### `[noexcept]` qsizetype QHash::count(const Key &key) const

Returns the number of items associated with the key.

See also contains().

### `[noexcept]` qsizetype QHash::count() const

Same as size().

This is an overloaded function.

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

### template <typename... Args> QHash<Key, T>::iterator QHash::emplace(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 the element already exists in the container, it is replaced.

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.

### `[noexcept]` bool QHash::empty() const

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

### `[noexcept]` QHash<Key, T>::iterator QHash::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]` QHash<Key, T>::const_iterator QHash::end() const

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 is an overloaded function.

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

Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator to the next item in the hash.

This function never causes QHash to rehash its internal data structure. This means that it can safely be called while iterating, and won't affect the order of items in the hash. For example:

     QHash<QObject *, int> objectHash;
     //...
     QHash<QObject *, int>::iterator i = objectHash.find(obj);
     while (i != objectHash.end() && i.key() == obj) {
         if (i.value() == 0) {
             i = objectHash.erase(i);
         } else {
             ++i;
         }
     }

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 remove(), take(), and find().

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

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

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

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

     QHash<QString, int> hash;
     //...
     QHash<QString, int>::const_iterator i = hash.find("HDR");
     while (i != hash.end() && i.key() == "HDR") {
         cout << i.value() << endl;
         ++i;
     }

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

### `[noexcept]` QHash<Key, T>::const_iterator QHash::find(const Key &key) const

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 is an overloaded function.

### void QHash::insert(const QHash<Key, T> &other)

Inserts all the items in the other hash into this hash.

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

### QHash<Key, T>::iterator QHash::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 key, that item's value is replaced with value.

Inserting a key/value pair with an existing key replaces the existing 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.

### `[since 6.11]` QHash<Key, T>::iterator QHash::insert(const Key &key, T &&value)

### `[since 6.11]` QHash<Key, T>::iterator QHash::insert(Key &&key, const T &value)

### `[since 6.11]` QHash<Key, T>::iterator QHash::insert(Key &&key, T &&value)

These functions were introduced in Qt 6.11.

### `[since 6.9]` template <typename Value> QHash<Key, T>::TryEmplaceResult QHash::insertOrAssign(const Key &key, Value &&value)

### `[since 6.9]` template <typename Value> QHash<Key, T>::TryEmplaceResult QHash::insertOrAssign(Key &&key, Value &&value)

### `[since 6.9]` template < typename K, typename Value, QHash<Key, T>::if_heterogeneously_searchable<K> = true, QHash<Key, T>::if_key_constructible_from<K> = true > QHash<Key, T>::TryEmplaceResult QHash::insertOrAssign(K &&key, Value &&value)

Attempts to insert an item with the key and value. If an item with key already exists its value is overwritten with value.

Returns an instance of TryEmplaceResult, a structure that holds an iterator to the item, and a boolean, inserted, denoting whether the item was newly created (`true`) or if it previously existed (`false`).

These functions were introduced in Qt 6.9.

See also insert(), tryEmplace(), and tryInsert().

### `[since 6.9]` template <typename Value> std::pair<QHash<Key, T>::key_value_iterator, bool> QHash::insert_or_assign(const Key &key, Value &&value)

### `[since 6.9]` template <typename Value> std::pair<QHash<Key, T>::key_value_iterator, bool> QHash::insert_or_assign(Key &&key, Value &&value)

### `[since 6.9]` template < typename K, typename Value, QHash<Key, T>::if_heterogeneously_searchable<K> = true, QHash<Key, T>::if_key_constructible_from<K> = true > std::pair<QHash<Key, T>::key_value_iterator, bool> QHash::insert_or_assign(K &&key, Value &&value)

Attempts to insert an item with the key and value. If an item with key already exists its value is overwritten with value.

Returns a pair consisting of an iterator pointing to the item, and a boolean, denoting whether the item was newly created (`true`) or if it previously existed (`false`).

These functions are provided for compatibility with the standard library.

These functions were introduced in Qt 6.9.

See also insert(), tryEmplace(), tryInsert(), and insertOrAssign().

### `[since 6.9]` template <typename Value> QHash<Key, T>::key_value_iterator QHash::insert_or_assign(QHash<Key, T>::const_iterator hint, const Key &key, Value &&value)

### `[since 6.9]` template <typename Value> QHash<Key, T>::key_value_iterator QHash::insert_or_assign(QHash<Key, T>::const_iterator hint, Key &&key, Value &&value)

### `[since 6.9]` template < typename K, typename Value, QHash<Key, T>::if_heterogeneously_searchable<K> = true, QHash<Key, T>::if_key_constructible_from<K> = true > QHash<Key, T>::key_value_iterator QHash::insert_or_assign(QHash<Key, T>::const_iterator hint, K &&key, Value &&value)

Attempts to insert an item with the key and value. If an item with key already exists its value is overwritten with value.

Returns a pair consisting of an iterator pointing to the item, and a boolean, denoting whether the item was newly created (`true`) or if it previously existed (`false`).

hint is ignored.

These functions are provided for compatibility with the standard library.

These functions were introduced in Qt 6.9.

See also insert(), tryEmplace(), and insertOrAssign().

### `[noexcept]` bool QHash::isEmpty() const

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

See also size().

### `[noexcept]` Key QHash::key(const T &value) const

### `[noexcept]` Key QHash::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 QHash's internal data structure is optimized for fast lookup by key, not by value.

### `[noexcept]` QHash<Key, T>::key_iterator QHash::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]` QHash<Key, T>::key_iterator QHash::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().

### QHash<Key, T>::key_value_iterator QHash::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]` QHash<Key, T>::const_key_value_iterator QHash::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().

### QHash<Key, T>::key_value_iterator QHash::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]` QHash<Key, T>::const_key_value_iterator QHash::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> QHash::keys() const

Returns a list containing all the keys in the hash, in an arbitrary 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> QHash::keys(const T &value) const

Returns a list containing all the keys associated with value value, in an arbitrary order.

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

This is an overloaded function.

### `[noexcept]` float QHash::load_factor() const

Returns the current load factor of the QHash's internal hash table. This is the same as capacity()/size(). The implementation used will aim to keep the load factor between 0.25 and 0.5. This avoids having too many hash table collisions that would degrade performance.

Even with a low load factor, the implementation of the hash table has a very low memory overhead.

This method purely exists for diagnostic purposes and you should rarely need to call it yourself.

See also reserve() and squeeze().

### bool QHash::remove(const Key &key)

Removes the item that has the key from the hash. Returns true if the key exists in the hash and the item has been removed, and false otherwise.

See also clear() and take().

### `[since 6.1]` template <typename Predicate> qsizetype QHash::removeIf(Predicate pred)

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

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

### void QHash::reserve(qsizetype size)

Ensures that the QHash's internal hash table has space to store at least size items without having to grow the hash table.

This implies that the hash table will contain at least 2 * size buckets to ensure good performance

This function is useful for code that needs to build a huge hash and wants to avoid repeated reallocation. For example:

     QHash<QString, int> hash;
     hash.reserve(20000);
     for (int i = 0; i < 20000; ++i)
         hash.insert(keys[i], values[i]);

Ideally, size should be the maximum number of items expected in the hash. QHash will then choose the smallest possible number of buckets that will allow storing size items in the table without having to grow the internal hash table. If size is an underestimate, the worst that will happen is that the QHash will be a bit slower.

In general, you will rarely ever need to call this function. QHash's internal hash table automatically grows to provide good performance without wasting too much memory.

See also squeeze() and capacity().

### `[noexcept]` qsizetype QHash::size() const

Returns the number of items in the hash.

See also isEmpty() and count().

### void QHash::squeeze()

Reduces the size of the QHash's internal hash table to save memory.

The sole purpose of this function is to provide a means of fine tuning QHash's memory usage. In general, you will rarely ever need to call this function.

See also reserve() and capacity().

### `[noexcept]` void QHash::swap(QHash<Key, T> &other)

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

### T QHash::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 you don't use the return value, remove() is more efficient.

See also remove().

### `[since 6.9]` template <typename... Args> QHash<Key, T>::TryEmplaceResult QHash::tryEmplace(const Key &key, Args &&... args)

### `[since 6.9]` template <typename... Args> QHash<Key, T>::TryEmplaceResult QHash::tryEmplace(Key &&key, Args &&... args)

### `[since 6.9]` template < typename K, typename... Args, QHash<Key, T>::if_heterogeneously_searchable<K> = true, QHash<Key, T>::if_key_constructible_from<K> = true > QHash<Key, T>::TryEmplaceResult QHash::tryEmplace(K &&key, Args &&... args)

Inserts a new item with the key and a value constructed from args. If an item with key already exists, no insertion takes place.

Returns an instance of TryEmplaceResult, a structure that holds an iterator to the newly created item, or to the pre-existing item that prevented the insertion, and a boolean, inserted, denoting whether the insertion took place.

For example, this can be used to avoid the pattern of comparing old and new size or double-lookups. Where you might previously have written code like:

     QHash<int, MyType> hash;
     // [...]
     int myKey = getKey();
     qsizetype oldSize = hash.size();
     MyType &elem = hash[myKey];
     if (oldSize != hash.size()) // Size changed: new element!
         initialize(elem);
     // [use elem...]

You can instead write:

     QHash<int, MyType> hash;
     // [...]
     int myKey = getKey();
     auto result = hash.tryEmplace(myKey);
     if (result.inserted) // New element!
         initialize(*result.iterator);
     // [use result.iterator...]

These functions were introduced in Qt 6.9.

See also emplace(), tryInsert(), and insertOrAssign().

### `[since 6.9]` QHash<Key, T>::TryEmplaceResult QHash::tryInsert(const Key &key, const T &value)

### `[since 6.9]` template < typename K, QHash<Key, T>::if_heterogeneously_searchable<K> = true, QHash<Key, T>::if_key_constructible_from<K> = true > QHash<Key, T>::TryEmplaceResult QHash::tryInsert(K &&key, const T &value)

Inserts a new item with the key and a value of value. If an item with key already exists, no insertion takes place.

Returns an instance of TryEmplaceResult, a structure that holds an iterator to the newly created item, or to the pre-existing item that prevented the insertion, and a boolean, inserted, denoting whether the insertion took place.

These functions were introduced in Qt 6.9.

See also insert(), tryEmplace(), and insertOrAssign().

### `[since 6.9]` template <typename... Args> std::pair<QHash<Key, T>::key_value_iterator, bool> QHash::try_emplace(const Key &key, Args &&... args)

### `[since 6.9]` template <typename... Args> std::pair<QHash<Key, T>::key_value_iterator, bool> QHash::try_emplace(Key &&key, Args &&... args)

### `[since 6.9]` template < typename K, typename... Args, QHash<Key, T>::if_heterogeneously_searchable<K> = true, QHash<Key, T>::if_key_constructible_from<K> = true > std::pair<QHash<Key, T>::key_value_iterator, bool> QHash::try_emplace(K &&key, Args &&... args)

Inserts a new item with the key and a value constructed from args. If an item with key already exists, no insertion takes place.

Returns a pair consisting of an iterator to the inserted item (or to the item that prevented the insertion), and a bool denoting whether the insertion took place.

These functions are provided for compatibility with the standard library.

These functions were introduced in Qt 6.9.

See also emplace(), tryEmplace(), tryInsert(), and insertOrAssign().

### `[since 6.9]` template < typename K, typename... Args, QHash<Key, T>::if_heterogeneously_searchable<K> = true, QHash<Key, T>::if_key_constructible_from<K> = true > QHash<Key, T>::key_value_iterator QHash::try_emplace(QHash<Key, T>::const_iterator hint, K &&key, Args &&... args)

### `[since 6.9]` template <typename... Args> QHash<Key, T>::key_value_iterator QHash::try_emplace(QHash<Key, T>::const_iterator hint, const Key &key, Args &&... args)

### `[since 6.9]` template <typename... Args> QHash<Key, T>::key_value_iterator QHash::try_emplace(QHash<Key, T>::const_iterator hint, Key &&key, Args &&... args)

Inserts a new item with the key and a value constructed from args. If an item with key already exists, no insertion takes place.

Returns the iterator of the inserted item, or to the item that prevented the insertion.

hint is ignored.

These functions are provided for compatibility with the standard library.

These functions were introduced in Qt 6.9.

See also emplace(), tryEmplace(), tryInsert(), and insertOrAssign().

### `[noexcept]` T QHash::value(const Key &key) const

### `[noexcept]` T QHash::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.

### QList<T> QHash::values() const

Returns a list containing all the values in the hash, in an arbitrary order.

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

### `[noexcept]` QHash<Key, T> &QHash::operator=(QHash<Key, T> &&other)

Move-assigns other to this QHash instance.

### `[noexcept]` QHash<Key, T> &QHash::operator=(const QHash<Key, T> &other)

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

### T &QHash::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.

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

### `[noexcept]` const T QHash::operator[](const Key &key) const

Same as value().

This is an overloaded function.

## Related Non-Members

### `[since 6.1]` template < typename Key, typename T, typename Predicate > qsizetype erase_if(QHash<Key, T> &hash, Predicate pred)

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

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

### `[constexpr noexcept, since 6.5]` template <typename Enum, std::enable_if_t<std::is_enum_v<Enum>, bool> = true> size_t qHash(Enum key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

Note: Prior to Qt 6.5, unscoped enums relied on the integer overloads of this function due to implicit conversion to their underlying integer types. For scoped enums, you had to implement an overload yourself. This is still the backwards-compatible fix to remain compatible with older Qt versions.

This function was introduced in Qt 6.5.

### `[noexcept, since 6.0]` size_t qHash(QByteArrayView key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

This function was introduced in Qt 6.0.

### `[constexpr noexcept]` size_t qHash(uint key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept]` size_t qHash(ulong key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept]` size_t qHash(ushort key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept, since 6.0]` size_t qHash(wchar_t key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

This function was introduced in Qt 6.0.

### `[noexcept]` size_t qHash(QDate key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[noexcept]` size_t qHash(QLatin1StringView key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[noexcept, since 6.0]` size_t qHash(QPoint key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

This function was introduced in Qt 6.0.

### `[noexcept]` size_t qHash(QTime key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept, since 6.9]` template <typename T, std::enable_if_t<std::is_same_v<T, bool>, bool>> size_t qHash(T key, size_t seed)

Returns the hash value for key, using seed to seed the calculation.

Note: This is qHash(bool), constrained to accept only arguments of type bool, not arguments of types that merely convert to bool.

Note: In Qt versions prior to 6.9, this overload was unintendedly provided by an undocumented 1-to-2-arg qHash adapter template function, with identical behavior.

This function was introduced in Qt 6.9.

### `[constexpr noexcept]` size_t qHash(char key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept, since 6.0]` size_t qHash(char16_t key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

This function was introduced in Qt 6.0.

### `[constexpr noexcept, since 6.0]` size_t qHash(char32_t key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

This function was introduced in Qt 6.0.

### `[constexpr noexcept, since 6.0]` size_t qHash(char8_t key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

This function was introduced in Qt 6.0.

### `[noexcept]` size_t qHash(const QBitArray &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[noexcept]` size_t qHash(const QByteArray &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept]` size_t qHash(const QChar key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### size_t qHash(const QDateTime &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[noexcept(...)]` template <typename Key, typename T> size_t qHash(const QHash<Key, T> &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

Types `Key` and `T` must be supported by qHash().

Note: This function is noexcept when `noexcept(qHash(std::declval<Key&>())) && noexcept(qHash(std::declval<T&>()))` is `true`.

### `[noexcept(...)]` template <typename T> size_t qHash(const QSet<T> &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

Type `T` must be supported by qHash().

The hash value is independent of the order of elements in key, that is, sets that contain the same elements hash to the same value.

Note: This function is noexcept when `noexcept(qHashRangeCommutative(key.begin(), key.end(), seed))` is `true`.

### `[noexcept]` size_t qHash(const QString &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[since 6.0]` size_t qHash(const QTypeRevision &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

This function was introduced in Qt 6.0.

### `[noexcept]` size_t qHash(const QUrl &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### size_t qHash(const QVersionNumber &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[noexcept]` template <typename T> size_t qHash(const T *key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[noexcept(...)]` template <typename T1, typename T2> size_t qHash(const std::pair<T1, T2> &key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

Types `T1` and `T2` must be supported by qHash().

Note: This function is noexcept when `QHashPrivate::noexceptPairHash<T1, T2>()` is `true`.

### `[noexcept]` size_t qHash(double key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[noexcept]` size_t qHash(float key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept]` size_t qHash(int key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept]` size_t qHash(long key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[noexcept]` size_t qHash(long double key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept, since 6.8]` size_t qHash(qint128 key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

Note: This function is only available on platforms that support a native 128-bit integer type.

This function was introduced in Qt 6.8.

### `[constexpr noexcept]` size_t qHash(qint64 key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept, since 6.8]` size_t qHash(quint128 key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

Note: This function is only available on platforms that support a native 128-bit integer type.

This function was introduced in Qt 6.8.

### `[constexpr noexcept]` size_t qHash(quint64 key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept]` size_t qHash(short key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept]` size_t qHash(signed char key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[constexpr noexcept, since 6.0]` size_t qHash(std::nullptr_t key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

This function was introduced in Qt 6.0.

### `[constexpr noexcept]` size_t qHash(uchar key, size_t seed = 0)

Returns the hash value for key, using seed to seed the calculation.

### `[noexcept]` size_t qHashBits(const void *p, size_t len, size_t seed = 0)

Returns the hash value for the memory block of size len pointed to by p, using seed to seed the calculation.

Use this function only to implement qHash() for your own custom types. For example, here's how you could implement a qHash() overload for std::vector<int>:

     inline size_t qHash(const std::vector<int> &key, size_t seed = 0)
     {
         if (key.empty())
             return seed;
         else
             return qHashBits(&key.front(), key.size() * sizeof(int), seed);
     }

This takes advantage of the fact that std::vector lays out its data contiguously. If that is not the case, or the contained type has padding, you should use qHashRange() instead.

It bears repeating that the implementation of qHashBits() - like the qHash() overloads offered by Qt - may change at any time. You must not rely on the fact that qHashBits() will give the same results (for the same inputs) across different Qt versions.

See also qHashRange() and qHashRangeCommutative().

### `[constexpr noexcept(...), since 6.0]` template <typename... T> size_t qHashMulti(size_t seed, const T &... args)

Returns the hash value for the args, using seed to seed the calculation, by successively applying qHash() to each element and combining the hash values into a single one.

Note that the order of the arguments is significant. If order does not matter, use qHashMultiCommutative() instead. If you are hashing raw memory, use qHashBits(); if you are hashing a range, use qHashRange().

This function is provided as a convenience to implement qHash() for your own custom types. For example, here's how you could implement a qHash() overload for a class `Employee`:

     #ifndef EMPLOYEE_H
     #define EMPLOYEE_H

     class Employee
     {
     public:
         Employee() {}
         Employee(const QString &name, QDate dateOfBirth);
         QString name() const { return myName; }
         QDate dateOfBirth() const { return myDateOfBirth; }
         //...

     private:
         QString myName;
         QDate myDateOfBirth;
     };

     inline bool operator==(const Employee &e1, const Employee &e2)
     {
         return e1.name() == e2.name()
                && e1.dateOfBirth() == e2.dateOfBirth();
     }

     inline size_t qHash(const Employee &key, size_t seed)
     {
         return qHashMulti(seed, key.name(), key.dateOfBirth());
     }

     #endif // EMPLOYEE_H

This function was introduced in Qt 6.0.

Note: This function is noexcept when `std::conjunction_v<QtPrivate::QNothrowHashable<T>...>` is `true`.

See also qHashMultiCommutative and qHashRange.

### `[constexpr noexcept(...), since 6.0]` template <typename... T> size_t qHashMultiCommutative(size_t seed, const T &... args)

Returns the hash value for the args, using seed to seed the calculation, by successively applying qHash() to each element and combining the hash values into a single one.

The order of the arguments is insignificant. If order does matter, use qHashMulti() instead, as it may produce better quality hashing. If you are hashing raw memory, use qHashBits(); if you are hashing a range, use qHashRange().

This function is provided as a convenience to implement qHash() for your own custom types.

This function was introduced in Qt 6.0.

Note: This function is noexcept when `std::conjunction_v<QtPrivate::QNothrowHashable<T>...>` is `true`.

See also qHashMulti and qHashRange.

### `[noexcept(...)]` template <typename InputIterator> size_t qHashRange(InputIterator first, InputIterator last, size_t seed = 0)

Returns the hash value for the range [first,last), using seed to seed the calculation, by successively applying qHash() to each element and combining the hash values into a single one.

The return value of this function depends on the order of elements in the range. That means that

     {0, 1, 2}

and

     {1, 2, 0}

hash to different values. If order does not matter, for example for hash tables, use qHashRangeCommutative() instead. If you are hashing raw memory, use qHashBits().

Use this function only to implement qHash() for your own custom types. For example, here's how you could implement a qHash() overload for std::vector<int>:

     inline size_t qHash(const std::vector<int> &key, size_t seed = 0)
     {
         return qHashRange(key.begin(), key.end(), seed);
     }

It bears repeating that the implementation of qHashRange() - like the qHash() overloads offered by Qt - may change at any time. You must not rely on the fact that qHashRange() will give the same results (for the same inputs) across different Qt versions, even if qHash() for the element type would.

Note: This function is noexcept when `noexcept(qHash(*first, 0))` is `true`.

See also qHashBits() and qHashRangeCommutative().

### `[noexcept(...)]` template <typename InputIterator> size_t qHashRangeCommutative(InputIterator first, InputIterator last, size_t seed = 0)

Returns the hash value for the range [first,last), using seed to seed the calculation, by successively applying qHash() to each element and combining the hash values into a single one.

The return value of this function does not depend on the order of elements in the range. That means that

     {0, 1, 2}

and

     {1, 2, 0}

hash to the same values. If order matters, for example, for vectors and arrays, use qHashRange() instead. If you are hashing raw memory, use qHashBits().

Use this function only to implement qHash() for your own custom types. For example, here's how you could implement a qHash() overload for std::unordered_set<int>:

     inline size_t qHash(const std::unordered_set<int> &key, size_t seed = 0)
     {
         return qHashRangeCommutative(key.begin(), key.end(), seed);
     }

It bears repeating that the implementation of qHashRangeCommutative() - like the qHash() overloads offered by Qt - may change at any time. You must not rely on the fact that qHashRangeCommutative() will give the same results (for the same inputs) across different Qt versions, even if qHash() for the element type would.

Note: This function is noexcept when `noexcept(qHash(*first, 0))` is `true`.

See also qHashBits() and qHashRange().

### `[noexcept]` bool operator!=(const QHash<Key, T> &lhs, const QHash<Key, T> &rhs)

Returns `true` if lhs hash is not equal to rhs hash; otherwise returns `false`.

Two hashes are considered equal if they contain the same (key, value) pairs.

This function requires the value type to implement `operator==()`.

See also operator==().

### template <typename Key, typename T> QDataStream &operator<<(QDataStream &out, const QHash<Key, T> &hash)

Writes the hash hash to stream out.

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

See also Serializing Qt Data Types.

### `[noexcept]` bool operator==(const QHash<Key, T> &lhs, const QHash<Key, T> &rhs)

Returns `true` if lhs hash is equal to rhs hash; otherwise returns `false`.

Two hashes are considered equal if they contain the same (key, value) pairs.

This function requires the value type to implement `operator==()`.

See also operator!=().

### template <typename Key, typename T> QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash)

Reads a hash from stream in into hash.

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

See also Serializing Qt Data Types.

## Macro Documentation

### `[since 6.11]` QT_NO_SINGLE_ARGUMENT_QHASH_OVERLOAD

Defining this macro disables the support for qHash overloads that only take one argument; in other words, for qHash overloads that do not also accept a seed. Support for the single-argument overloads of qHash is deprecated and will be removed in Qt 7.

This macro was introduced in Qt 6.11.

See also qHash.