6.0.0-rc4Source

QHash Class

template <typename K, typename V> class QHash

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

Header#include <QHash>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
InheritsQtPrivate::AssocContainer

List of all members, including inherited members

Public Functions

QHash()
voidreserve(qsizetype n)
qsizetypecapacity() const
voidsqueeze()
std::size_tbucket_count() const
std::size_tmax_bucket_count() const
floatload_factor() const
floatmax_load_factor() const
TryEmplaceResulttryEmplace(const K &k, Args &&...args)
TryEmplaceResulttryInsert(const K &k, const V &v)
TryEmplaceResultinsertOrAssign(const K &k, Value &&v)
std::pair<typename Base::key_value_iterator, bool>try_emplace(const K &k, Args &&...args)
std::pair<typename Base::key_value_iterator, bool>insert_or_assign(const K &k, Value &&v)

Detailed Description

QHash<Key, T> stores key-value pairs and finds values by key in constant time on average. Each key appears at most once; inserting an existing key replaces its value. Items are stored in no particular order. Use QMultiHash to store several values under one key. The key type needs operator== and either a qHash() overload or a std::hash specialization.

QHash<QString, int> stock;
stock.insert("apple", 3);
stock["pear"] = 7;
int apples = stock.value("apple");

The size, lookup, insertion, removal and iteration functions are shared with the other associative containers and behave as they do in Qt. keyValueBegin(), keyValueEnd() and asKeyValueRange() visit the key and value pairs, as described in QKeyValueIterator.

See also QMultiHash, QMap and QSet.

Differences from Qt

QHash is implemented on top of std::unordered_map and has no implicit sharing: copying a hash copies all of its elements. The iteration order and the value returned by capacity() depend on the standard library.

Member Function Documentation

QHash::QHash()

Constructs an empty hash.

std::size_t QHash::bucket_count() const

Returns the number of buckets in the hash table. Provided for compatibility with the standard library.

See also capacity() and load_factor().

qsizetype QHash::capacity() const

Returns the number of buckets in the hash table.

See also reserve().

template <typename Value>std::pair<typename Base::key_value_iterator, bool> QHash::insert_or_assign(const K &k, Value &&v)

Does the same as insertOrAssign() with the key k and the value v, and returns the item and whether it was inserted as a std::pair. Provided for compatibility with the standard library.

This function was introduced in 6.9.

template <typename Value>TryEmplaceResult QHash::insertOrAssign(const K &k, Value &&v)

Inserts an item with the key k and the value v, or assigns v to the item that has that key already. Returns the item and whether it was inserted.

This function was introduced in 6.9.

See also tryInsert().

float QHash::load_factor() const

Returns the average number of items per bucket.

See also max_load_factor() and bucket_count().

std::size_t QHash::max_bucket_count() const

Returns the largest number of buckets the hash table can have.

See also bucket_count().

float QHash::max_load_factor() const

Returns the load factor above which the hash table grows.

See also load_factor().

void QHash::reserve(qsizetype n)

Prepares the hash for holding at least n items without rehashing.

See also capacity().

void QHash::squeeze()

Shrinks the hash table to the smallest size that holds the items.

See also capacity().

template <typename... Args>std::pair<typename Base::key_value_iterator, bool> QHash::try_emplace(const K &k, Args &&...args)

Does the same as tryEmplace() with the key k and args, and returns the item and whether it was inserted as a std::pair. Provided for compatibility with the standard library.

This function was introduced in 6.9.

template <typename... Args>TryEmplaceResult QHash::tryEmplace(const K &k, Args &&...args)

Inserts an item with the key k and a value constructed from args, unless the hash has an item with that key already; an existing value is left unchanged. Returns the item and whether it was inserted.

This function was introduced in 6.9.

See also tryInsert() and insertOrAssign().

TryEmplaceResult QHash::tryInsert(const K &k, const V &v)

Inserts an item with the key k and the value v, unless the hash has an item with that key already; an existing value is left unchanged. Returns the item and whether it was inserted.

This function was introduced in 6.9.

See also tryEmplace() and insertOrAssign().