Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QHash< Key, T > Class Template Reference

\inmodule QtCore More...

#include <qhash.h>

+ Collaboration diagram for QHash< Key, T >:

Classes

class  const_iterator
 \inmodule QtCore More...
 
class  iterator
 \inmodule QtCore More...
 
class  key_iterator
 \inmodule QtCore More...
 
class  TryEmplaceResult
 \inmodule QtCore More...
 

Public Types

using key_type = Key
 Typedef for Key.
 
using mapped_type = T
 Typedef for T.
 
using value_type = T
 
using size_type = qsizetype
 Typedef for int.
 
using difference_type = qsizetype
 Typedef for ptrdiff_t.
 
using reference = T &
 
using const_reference = const T &
 
typedef QKeyValueIterator< const Key &, const T &, const_iteratorconst_key_value_iterator
 \inmodule QtCore
 
typedef QKeyValueIterator< const Key &, T &, iteratorkey_value_iterator
 \inmodule QtCore
 
typedef iterator Iterator
 Qt-style synonym for QHash::iterator.
 
typedef const_iterator ConstIterator
 Qt-style synonym for QHash::const_iterator.
 

Public Member Functions

 QHash () noexcept=default
 Constructs an empty hash.
 
 QHash (std::initializer_list< std::pair< Key, T > > list)
 
 QHash (const QHash &other) noexcept
 Constructs a copy of other.
 
 ~QHash ()
 Destroys the hash.
 
QHashoperator= (const QHash &other) noexcept(std::is_nothrow_destructible< Node >::value)
 Assigns other to this hash and returns a reference to this hash.
 
 QHash (QHash &&other) noexcept
 Move-constructs a QHash instance, making it point at the same object that other was pointing to.
 
template<typename InputIterator, QtPrivate::IfAssociativeIteratorHasKeyAndValue< InputIterator > = true>
 QHash (InputIterator f, InputIterator l)
 
template<typename InputIterator, QtPrivate::IfAssociativeIteratorHasFirstAndSecond< InputIterator > = true>
 QHash (InputIterator f, InputIterator l)
 
void swap (QHash &other) noexcept
 
template<typename Predicate>
qsizetype removeIf (Predicate pred)
 
take (const Key &key)
 Removes the item with the key from the hash and returns the value associated with it.
 
bool contains (const Key &key) const noexcept
 Returns true if the hash contains an item with the key; otherwise returns false.
 
qsizetype count (const Key &key) const noexcept
 Returns the number of items associated with the key.
 
Key key (const T &value) const noexcept
 
Key key (const T &value, const Key &defaultKey) const noexcept
 
value (const Key &key) const noexcept
 
value (const Key &key, const T &defaultValue) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the value associated with the key.
 
T & operator[] (const Key &key)
 Returns the value associated with the key as a modifiable reference.
 
const T operator[] (const Key &key) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Same as value().
 
QList< Keykeys () const
 Returns a list containing all the keys in the hash, in an arbitrary order.
 
QList< Keykeys (const T &value) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a list containing all the keys associated with value value, in an arbitrary order.
 
QList< T > values () const
 Returns a list containing all the values in the hash, in an arbitrary order.
 
iterator begin ()
 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
 
const_iterator begin () const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const_iterator cbegin () const noexcept
 
const_iterator constBegin () const noexcept
 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
 
iterator end () noexcept
 Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the hash.
 
const_iterator end () const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const_iterator cend () const noexcept
 
const_iterator constEnd () const noexcept
 Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the hash.
 
key_iterator keyBegin () const noexcept
 
key_iterator keyEnd () const noexcept
 
key_value_iterator keyValueBegin ()
 
key_value_iterator keyValueEnd ()
 
const_key_value_iterator keyValueBegin () const noexcept
 
const_key_value_iterator constKeyValueBegin () const noexcept
 
const_key_value_iterator keyValueEnd () const noexcept
 
const_key_value_iterator constKeyValueEnd () const noexcept
 
auto asKeyValueRange () &
 
auto asKeyValueRange () const &
 
auto asKeyValueRange () &&
 
auto asKeyValueRange () const &&
 
iterator erase (const_iterator it)
 
std::pair< iterator, iteratorequal_range (const Key &key)
 
std::pair< const_iterator, const_iteratorequal_range (const Key &key) const noexcept
 
qsizetype count () const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Same as size().
 
iterator find (const Key &key)
 Returns an iterator pointing to the item with the key in the hash.
 
const_iterator find (const Key &key) const noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const_iterator constFind (const Key &key) const noexcept
 
iterator insert (const Key &key, const T &value)
 Inserts a new item with the key and a value of value.
 
void insert (const QHash &hash)
 
template<typename ... Args>
iterator emplace (const Key &key, Args &&... args)
 
template<typename ... Args>
iterator emplace (Key &&key, Args &&... args)
 Inserts a new element into the container.
 
template<typename... Args>
TryEmplaceResult tryEmplace (const Key &key, Args &&...args)
 \variable QHash::TryEmplaceResult::iterator
 
template<typename... Args>
TryEmplaceResult tryEmplace (Key &&key, Args &&...args)
 
TryEmplaceResult tryInsert (const Key &key, const T &value)
 
template<typename... Args>
std::pair< key_value_iterator, bool > try_emplace (const Key &key, Args &&...args)
 
template<typename... Args>
std::pair< key_value_iterator, bool > try_emplace (Key &&key, Args &&...args)
 
template<typename... Args>
key_value_iterator try_emplace (const_iterator, const Key &key, Args &&...args)
 
template<typename... Args>
key_value_iterator try_emplace (const_iterator, Key &&key, Args &&...args)
 
template<typename Value>
TryEmplaceResult insertOrAssign (const Key &key, Value &&value)
 
template<typename Value>
TryEmplaceResult insertOrAssign (Key &&key, Value &&value)
 
template<typename Value>
std::pair< key_value_iterator, bool > insert_or_assign (const Key &key, Value &&value)
 
template<typename Value>
std::pair< key_value_iterator, bool > insert_or_assign (Key &&key, Value &&value)
 
template<typename Value>
key_value_iterator insert_or_assign (const_iterator, const Key &key, Value &&value)
 
template<typename Value>
key_value_iterator insert_or_assign (const_iterator, Key &&key, Value &&value)
 
float load_factor () const noexcept
 Returns the current load factor of the QHash's internal hash table.
 
size_t bucket_count () const noexcept
 
bool empty () const noexcept
 This function is provided for STL compatibility.
 
template<typename K, if_heterogeneously_searchable< K > = true>
bool remove (const K &key)
 
template<typename K, if_heterogeneously_searchable< K > = true>
take (const K &key)
 
template<typename K, if_heterogeneously_searchable< K > = true>
bool contains (const K &key) const
 
template<typename K, if_heterogeneously_searchable< K > = true>
qsizetype count (const K &key) const
 
template<typename K, if_heterogeneously_searchable< K > = true>
value (const K &key) const noexcept
 
template<typename K, if_heterogeneously_searchable< K > = true>
value (const K &key, const T &defaultValue) const noexcept
 
template<typename K, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
T & operator[] (const K &key)
 
template<typename K, if_heterogeneously_searchable< K > = true>
const T operator[] (const K &key) const noexcept
 
template<typename K, if_heterogeneously_searchable< K > = true>
std::pair< iterator, iteratorequal_range (const K &key)
 
template<typename K, if_heterogeneously_searchable< K > = true>
std::pair< const_iterator, const_iteratorequal_range (const K &key) const noexcept
 
template<typename K, if_heterogeneously_searchable< K > = true>
iterator find (const K &key)
 
template<typename K, if_heterogeneously_searchable< K > = true>
const_iterator find (const K &key) const noexcept
 
template<typename K, if_heterogeneously_searchable< K > = true>
const_iterator constFind (const K &key) const noexcept
 
template<typename K, typename... Args, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
TryEmplaceResult tryEmplace (K &&key, Args &&...args)
 
template<typename K, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
TryEmplaceResult tryInsert (K &&key, const T &value)
 
template<typename K, typename... Args, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
std::pair< key_value_iterator, bool > try_emplace (K &&key, Args &&...args)
 
template<typename K, typename... Args, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
key_value_iterator try_emplace (const_iterator, K &&key, Args &&...args)
 
template<typename K, typename Value, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
TryEmplaceResult insertOrAssign (K &&key, Value &&value)
 
template<typename K, typename Value, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
std::pair< key_value_iterator, bool > insert_or_assign (K &&key, Value &&value)
 
template<typename K, typename Value, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
key_value_iterator insert_or_assign (const_iterator, K &&key, Value &&value)
 

Static Public Member Functions

static float max_load_factor () noexcept
 
static size_t max_bucket_count () noexcept
 

Friends

class QSet< Key >
 
class QMultiHash< Key, T >
 
class iterator
 
class const_iterator
 
template<typename AKey = Key, typename AT = T, QTypeTraits::compare_eq_result_container< QHash, AKey, AT > = true>
bool comparesEqual (const QHash &lhs, const QHash &rhs) noexcept
 

Related Symbols

(Note that these are not member symbols.)

template< typename... T > size_t qHashMulti (size_t seed, const T &...args)
 
template< typename... T > size_t qHashMultiCommutative (size_t seed, const T &...args)
 
template< typename InputIterator > size_t qHashRange (InputIterator first, InputIterator last, size_t seed=0)
 
template< typename InputIterator > size_t qHashRangeCommutative (InputIterator first, InputIterator last, size_t seed=0)
 
size_t qHashBits (const void *p, size_t len, size_t seed=0)
 
template< typename T > bool qHashEquals (const T &a, const T &b)
 
template< class Key, class T > QDataStreamoperator<< (QDataStream &out, const QHash< Key, T > &hash)
 Writes the hash hash to stream out.
 
template< class Key, class T > QDataStreamoperator>> (QDataStream &in, QHash< Key, T > &hash)
 Reads a hash from stream in into hash.
 
template< typename Key, typename T, typename Predicate > qsizetype erase_if (QHash< Key, T > &hash, Predicate pred)
 
size_t qHash (const QGeoCoordinate &coordinate, size_t seed=0)
 Returns a hash value for coordinate, using seed to seed the calculation.
 

Detailed Description

template<typename Key, typename T>
class QHash< Key, T >

\inmodule QtCore

The QHash class is a template class that provides a hash-table-based dictionary. \compares equality

\reentrant

QHash<Key, T> is one of Qt's generic \l{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:

\list

  • QHash provides faster lookups than QMap. (See \l{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 \l{qHash}). \endlist

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

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 \l{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
...
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().

If you want to navigate through all the (key, value) pairs stored in a QHash, you can use an iterator. QHash provides both \l{Java-style iterators} (QHashIterator and QMutableHashIterator) and \l{STL-style iterators} (QHash::const_iterator and QHash::iterator). Here's how to iterate over a QHash<QString, int> using a Java-style iterator:

QHashIterator<QString, int> i(hash);
while (i.hasNext()) {
i.next();
cout << qPrintable(i.key()) << ": " << i.value() << endl;
}

Here's the same code, but using an STL-style iterator:

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

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 \l{QMultiHash}.

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

...
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 \l{assignable data types}. You cannot, for example, store a QWidget as a value; instead, store a QWidget *.

\target qHash

Definition at line 836 of file qhash.h.

Member Typedef Documentation

◆ const_key_value_iterator

template<typename Key, typename T>
typedef QKeyValueIterator<const Key&, const T&, const_iterator> QHash< Key, T >::const_key_value_iterator

\inmodule QtCore

Since
5.10

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

Definition at line 1240 of file qhash.h.

◆ const_reference

template<typename Key, typename T>
using QHash< Key, T >::const_reference = const T &

Definition at line 853 of file qhash.h.

◆ ConstIterator

template<typename Key, typename T>
typedef const_iterator QHash< Key, T >::ConstIterator

Qt-style synonym for QHash::const_iterator.

Definition at line 1345 of file qhash.h.

◆ difference_type

template<typename Key, typename T>
using QHash< Key, T >::difference_type = qsizetype

Typedef for ptrdiff_t.

Provided for STL compatibility.

Definition at line 851 of file qhash.h.

◆ Iterator

template<typename Key, typename T>
typedef iterator QHash< Key, T >::Iterator

Qt-style synonym for QHash::iterator.

Definition at line 1344 of file qhash.h.

◆ key_type

template<typename Key, typename T>
using QHash< Key, T >::key_type = Key

Typedef for Key.

Provided for STL compatibility.

Definition at line 847 of file qhash.h.

◆ key_value_iterator

template<typename Key, typename T>
typedef QKeyValueIterator<const Key&, T&, iterator> QHash< Key, T >::key_value_iterator

\inmodule QtCore

Since
5.10

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

Definition at line 1241 of file qhash.h.

◆ mapped_type

template<typename Key, typename T>
using QHash< Key, T >::mapped_type = T

Typedef for T.

Provided for STL compatibility.

Definition at line 848 of file qhash.h.

◆ reference

template<typename Key, typename T>
using QHash< Key, T >::reference = T &

Definition at line 852 of file qhash.h.

◆ size_type

template<typename Key, typename T>
using QHash< Key, T >::size_type = qsizetype

Typedef for int.

Provided for STL compatibility.

Definition at line 850 of file qhash.h.

◆ value_type

template<typename Key, typename T>
using QHash< Key, T >::value_type = T

Definition at line 849 of file qhash.h.

Constructor & Destructor Documentation

◆ QHash() [1/6]

template<typename Key, typename T>
QHash< Key, T >::QHash ( )
inlinedefaultnoexcept

Constructs an empty hash.

See also
clear()

◆ QHash() [2/6]

template<typename Key, typename T>
QHash< Key, T >::QHash ( std::initializer_list< std::pair< Key, T > > list)
inline
Since
5.1

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

Definition at line 856 of file qhash.h.

◆ QHash() [3/6]

template<typename Key, typename T>
QHash< Key, T >::QHash ( const QHash< Key, T > & other)
inlinenoexcept

Constructs a copy of other.

This operation occurs in \l{constant time}, because QHash is \l{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 \l{linear time}.

See also
operator=()

Definition at line 862 of file qhash.h.

◆ ~QHash()

template<typename Key, typename T>
QHash< Key, T >::~QHash ( )
inline

Destroys the hash.

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

Definition at line 868 of file qhash.h.

◆ QHash() [4/6]

template<typename Key, typename T>
QHash< Key, T >::QHash ( QHash< Key, T > && other)
inlinenoexcept

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

Since
5.2

Definition at line 890 of file qhash.h.

Referenced by Iterator<, int >::QHash().

+ Here is the caller graph for this function:

◆ QHash() [5/6]

template<typename Key, typename T>
template<typename InputIterator, QtPrivate::IfAssociativeIteratorHasKeyAndValue< InputIterator > = true>
QHash< Key, T >::QHash ( InputIterator begin,
InputIterator end )
inline
Since
5.14

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.

Definition at line 900 of file qhash.h.

◆ QHash() [6/6]

template<typename Key, typename T>
template<typename InputIterator, QtPrivate::IfAssociativeIteratorHasFirstAndSecond< InputIterator > = true>
QHash< Key, T >::QHash ( InputIterator f,
InputIterator l )
inline

Definition at line 909 of file qhash.h.

Member Function Documentation

◆ asKeyValueRange() [1/4]

template<typename Key, typename T>
auto QHash< Key, T >::asKeyValueRange ( ) &
inline

Definition at line 1260 of file qhash.h.

◆ asKeyValueRange() [2/4]

template<typename Key, typename T>
auto QHash< Key, T >::asKeyValueRange ( ) &&
inline

Definition at line 1262 of file qhash.h.

◆ asKeyValueRange() [3/4]

template<typename Key, typename T>
auto QHash< Key, T >::asKeyValueRange ( ) const &
inline

Definition at line 1261 of file qhash.h.

◆ asKeyValueRange() [4/4]

template<typename Key, typename T>
auto QHash< Key, T >::asKeyValueRange ( ) const &&
inline

Definition at line 1263 of file qhash.h.

◆ begin() [1/2]

template<typename Key, typename T>
iterator QHash< Key, T >::begin ( )
inline

Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.

qhash-iterator-invalidation-func-desc

See also
constBegin(), end()

Definition at line 1244 of file qhash.h.

◆ begin() [2/2]

template<typename Key, typename T>
const_iterator QHash< Key, T >::begin ( ) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

qhash-iterator-invalidation-func-desc

Definition at line 1245 of file qhash.h.

Referenced by Iterator<, int >::comparesEqual, Iterator<, int >::constKeyValueBegin(), Iterator<, int >::insert(), Iterator<, int >::keyBegin(), Iterator<, int >::keyValueBegin(), and Iterator<, int >::keyValueBegin().

+ Here is the caller graph for this function:

◆ bucket_count()

template<typename Key, typename T>
size_t QHash< Key, T >::bucket_count ( ) const
inlinenoexcept

Definition at line 1515 of file qhash.h.

◆ cbegin()

template<typename Key, typename T>
const_iterator QHash< Key, T >::cbegin ( ) const
inlinenoexcept
Since
5.0

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.

qhash-iterator-invalidation-func-desc

See also
begin(), cend()

Definition at line 1246 of file qhash.h.

◆ cend()

template<typename Key, typename T>
const_iterator QHash< Key, T >::cend ( ) const
inlinenoexcept
Since
5.0

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the hash.

qhash-iterator-invalidation-func-desc

See also
cbegin(), end()

Definition at line 1250 of file qhash.h.

◆ constBegin()

template<typename Key, typename T>
const_iterator QHash< Key, T >::constBegin ( ) const
inlinenoexcept

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.

qhash-iterator-invalidation-func-desc

See also
begin(), constEnd()

Definition at line 1247 of file qhash.h.

◆ constEnd()

template<typename Key, typename T>
const_iterator QHash< Key, T >::constEnd ( ) const
inlinenoexcept

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the hash.

qhash-iterator-invalidation-func-desc

See also
constBegin(), end()

Definition at line 1251 of file qhash.h.

Referenced by Iterator<, int >::erase().

+ Here is the caller graph for this function:

◆ constFind() [1/2]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
const_iterator QHash< Key, T >::constFind ( const K & key) const
inlinenoexcept

Definition at line 1609 of file qhash.h.

◆ constFind() [2/2]

template<typename Key, typename T>
const_iterator QHash< Key, T >::constFind ( const Key & key) const
inlinenoexcept
Since
4.1

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

qhash-iterator-invalidation-func-desc

See also
find()

Definition at line 1355 of file qhash.h.

◆ constKeyValueBegin()

template<typename Key, typename T>
const_key_value_iterator QHash< Key, T >::constKeyValueBegin ( ) const
inlinenoexcept
Since
5.10

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first entry in the hash.

qhash-iterator-invalidation-func-desc

See also
keyValueBegin()

Definition at line 1257 of file qhash.h.

◆ constKeyValueEnd()

template<typename Key, typename T>
const_key_value_iterator QHash< Key, T >::constKeyValueEnd ( ) const
inlinenoexcept
Since
5.10

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary entry after the last entry in the hash.

qhash-iterator-invalidation-func-desc

See also
constKeyValueBegin()

Definition at line 1259 of file qhash.h.

◆ contains() [1/2]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
bool QHash< Key, T >::contains ( const K & key) const
inline

Definition at line 1551 of file qhash.h.

◆ contains() [2/2]

template<typename Key, typename T>
bool QHash< Key, T >::contains ( const Key & key) const
inlinenoexcept

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

See also
count()

Definition at line 1040 of file qhash.h.

◆ count() [1/3]

template<typename Key, typename T>
qsizetype QHash< Key, T >::count ( ) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Same as size().

Definition at line 1346 of file qhash.h.

◆ count() [2/3]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
qsizetype QHash< Key, T >::count ( const K & key) const
inline

Definition at line 1556 of file qhash.h.

◆ count() [3/3]

template<typename Key, typename T>
qsizetype QHash< Key, T >::count ( const Key & key) const
inlinenoexcept

Returns the number of items associated with the key.

See also
contains()

Definition at line 1046 of file qhash.h.

◆ emplace() [1/2]

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

Definition at line 1380 of file qhash.h.

◆ emplace() [2/2]

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

Inserts a new element into the container.

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

Returns an iterator pointing to the new element.

qhash-iterator-invalidation-func-desc

Definition at line 1387 of file qhash.h.

◆ empty()

template<typename Key, typename T>
bool QHash< Key, T >::empty ( ) const
inlinenodiscardnoexcept

This function is provided for STL compatibility.

It is equivalent to isEmpty(), returning true if the hash is empty; otherwise returns false.

Definition at line 1519 of file qhash.h.

◆ end() [1/2]

template<typename Key, typename T>
const_iterator QHash< Key, T >::end ( ) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

qhash-iterator-invalidation-func-desc

Definition at line 1249 of file qhash.h.

Referenced by Iterator<, int >::comparesEqual, Iterator<, int >::constKeyValueEnd(), Iterator<, int >::insert(), Iterator<, int >::keyEnd(), Iterator<, int >::keyValueEnd(), and Iterator<, int >::keyValueEnd().

+ Here is the caller graph for this function:

◆ end() [2/2]

template<typename Key, typename T>
iterator QHash< Key, T >::end ( )
inlinenoexcept

Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last item in the hash.

qhash-iterator-invalidation-func-desc

See also
begin(), constEnd()

Definition at line 1248 of file qhash.h.

◆ equal_range() [1/4]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
std::pair< iterator, iterator > QHash< Key, T >::equal_range ( const K & key)
inline

Definition at line 1588 of file qhash.h.

◆ equal_range() [2/4]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
std::pair< const_iterator, const_iterator > QHash< Key, T >::equal_range ( const K & key) const
inlinenoexcept

Definition at line 1594 of file qhash.h.

◆ equal_range() [3/4]

template<typename Key, typename T>
std::pair< iterator, iterator > QHash< Key, T >::equal_range ( const Key & key)
inline

Definition at line 1303 of file qhash.h.

◆ equal_range() [4/4]

template<typename Key, typename T>
std::pair< const_iterator, const_iterator > QHash< Key, T >::equal_range ( const Key & key) const
inlinenoexcept

Definition at line 1307 of file qhash.h.

◆ erase()

template<typename Key, typename T>
iterator QHash< Key, T >::erase ( const_iterator pos)
inline
Since
5.7

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>::iterator i = objectHash.find(obj);
while (i != objectHash.end() && i.key() == obj) {
if (i.value() == 0) {
i = objectHash.erase(i);
} else {
++i;
}
}

qhash-iterator-invalidation-func-desc

See also
remove(), take(), find()

Definition at line 1289 of file qhash.h.

◆ find() [1/4]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
iterator QHash< Key, T >::find ( const K & key)
inline

Definition at line 1599 of file qhash.h.

◆ find() [2/4]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
const_iterator QHash< Key, T >::find ( const K & key) const
inlinenoexcept

Definition at line 1604 of file qhash.h.

◆ find() [3/4]

template<typename Key, typename T>
iterator QHash< Key, T >::find ( const Key & key)
inline

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>::const_iterator i = hash.find("HDR");
while (i != hash.end() && i.key() == "HDR") {
cout << i.value() << endl;
++i;
}

qhash-iterator-invalidation-func-desc

See also
value(), values()

Definition at line 1347 of file qhash.h.

◆ find() [4/4]

template<typename Key, typename T>
const_iterator QHash< Key, T >::find ( const Key & key) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

qhash-iterator-invalidation-func-desc

Definition at line 1351 of file qhash.h.

◆ insert() [1/2]

template<typename Key, typename T>
iterator QHash< Key, T >::insert ( const Key & key,
const T & value )
inline

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.

Returns an iterator pointing to the new/updated element.

qhash-iterator-invalidation-func-desc

Definition at line 1359 of file qhash.h.

◆ insert() [2/2]

template<typename Key, typename T>
void QHash< Key, T >::insert ( const QHash< Key, T > & other)
inline
Since
5.15

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.

Definition at line 1364 of file qhash.h.

Referenced by Iterator<, int >::QHash().

+ Here is the caller graph for this function:

◆ insert_or_assign() [1/6]

template<typename Key, typename T>
template<typename Value>
std::pair< key_value_iterator, bool > QHash< Key, T >::insert_or_assign ( const Key & key,
Value && value )
inline

Definition at line 1481 of file qhash.h.

◆ insert_or_assign() [2/6]

template<typename Key, typename T>
template<typename Value>
key_value_iterator QHash< Key, T >::insert_or_assign ( const_iterator hint,
const Key & key,
Value && value )
inline

Definition at line 1491 of file qhash.h.

◆ insert_or_assign() [3/6]

template<typename Key, typename T>
template<typename K, typename Value, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
key_value_iterator QHash< Key, T >::insert_or_assign ( const_iterator hint,
K && key,
Value && value )
inline
Since
6.9

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.

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

Definition at line 1644 of file qhash.h.

◆ insert_or_assign() [4/6]

template<typename Key, typename T>
template<typename Value>
key_value_iterator QHash< Key, T >::insert_or_assign ( const_iterator hint,
Key && key,
Value && value )
inline

Definition at line 1496 of file qhash.h.

◆ insert_or_assign() [5/6]

template<typename Key, typename T>
template<typename K, typename Value, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
std::pair< key_value_iterator, bool > QHash< Key, T >::insert_or_assign ( K && key,
Value && value )
inline
Since
6.9

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.

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

Definition at line 1639 of file qhash.h.

◆ insert_or_assign() [6/6]

template<typename Key, typename T>
template<typename Value>
std::pair< key_value_iterator, bool > QHash< Key, T >::insert_or_assign ( Key && key,
Value && value )
inline

Definition at line 1486 of file qhash.h.

◆ insertOrAssign() [1/3]

template<typename Key, typename T>
template<typename Value>
TryEmplaceResult QHash< Key, T >::insertOrAssign ( const Key & key,
Value && value )
inline

Definition at line 1471 of file qhash.h.

◆ insertOrAssign() [2/3]

template<typename Key, typename T>
template<typename K, typename Value, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
TryEmplaceResult QHash< Key, T >::insertOrAssign ( K && key,
Value && value )
inline
Since
6.9

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 \l{TryEmplaceResult}, a structure that holds an \l{QHash::TryEmplaceResult::}{iterator} to the item, and a boolean, \l{QHash::TryEmplaceResult::}{inserted}, denoting whether the item was newly created ({true}) or if it previously existed ({false}).

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

Definition at line 1634 of file qhash.h.

◆ insertOrAssign() [3/3]

template<typename Key, typename T>
template<typename Value>
TryEmplaceResult QHash< Key, T >::insertOrAssign ( Key && key,
Value && value )
inline

Definition at line 1476 of file qhash.h.

◆ key() [1/2]

template<typename Key, typename T>
Key QHash< Key, T >::key ( const T & value) const
inlinenoexcept

Definition at line 1067 of file qhash.h.

◆ key() [2/2]

template<typename Key, typename T>
Key QHash< Key, T >::key ( const T & value,
const Key & defaultKey ) const
inlinenoexcept
Since
4.3

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

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

Definition at line 1074 of file qhash.h.

◆ keyBegin()

template<typename Key, typename T>
key_iterator QHash< Key, T >::keyBegin ( ) const
inlinenoexcept
Since
5.6

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first key in the hash.

qhash-iterator-invalidation-func-desc

See also
keyEnd()

Definition at line 1252 of file qhash.h.

◆ keyEnd()

template<typename Key, typename T>
key_iterator QHash< Key, T >::keyEnd ( ) const
inlinenoexcept
Since
5.6

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last key in the hash.

qhash-iterator-invalidation-func-desc

See also
keyBegin()

Definition at line 1253 of file qhash.h.

◆ keys() [1/2]

template<typename Key, typename T>
QList< Key > QHash< Key, T >::keys ( ) const
inline

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 \l {linear time}. The time and memory use that entails can be avoided by iterating from \l keyBegin() to \l keyEnd().

See also
values(), key()

Definition at line 1120 of file qhash.h.

◆ keys() [2/2]

template<typename Key, typename T>
QList< Key > QHash< Key, T >::keys ( const T & value) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns a list containing all the keys associated with value value, in an arbitrary order.

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

Definition at line 1121 of file qhash.h.

◆ keyValueBegin() [1/2]

template<typename Key, typename T>
key_value_iterator QHash< Key, T >::keyValueBegin ( )
inline
Since
5.10

Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first entry in the hash.

qhash-iterator-invalidation-func-desc

See also
keyValueEnd()

Definition at line 1254 of file qhash.h.

◆ keyValueBegin() [2/2]

template<typename Key, typename T>
const_key_value_iterator QHash< Key, T >::keyValueBegin ( ) const
inlinenoexcept
Since
5.10

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first entry in the hash.

qhash-iterator-invalidation-func-desc

See also
keyValueEnd()

Definition at line 1256 of file qhash.h.

◆ keyValueEnd() [1/2]

template<typename Key, typename T>
key_value_iterator QHash< Key, T >::keyValueEnd ( )
inline
Since
5.10

Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary entry after the last entry in the hash.

qhash-iterator-invalidation-func-desc

See also
keyValueBegin()

Definition at line 1255 of file qhash.h.

◆ keyValueEnd() [2/2]

template<typename Key, typename T>
const_key_value_iterator QHash< Key, T >::keyValueEnd ( ) const
inlinenoexcept
Since
5.10

Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary entry after the last entry in the hash.

qhash-iterator-invalidation-func-desc

See also
keyValueBegin()

Definition at line 1258 of file qhash.h.

◆ load_factor()

template<typename Key, typename T>
float QHash< Key, T >::load_factor ( ) const
inlinenoexcept

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

Definition at line 1513 of file qhash.h.

◆ max_bucket_count()

template<typename Key, typename T>
static size_t QHash< Key, T >::max_bucket_count ( )
inlinestaticnoexcept

Definition at line 1516 of file qhash.h.

◆ max_load_factor()

template<typename Key, typename T>
static float QHash< Key, T >::max_load_factor ( )
inlinestaticnoexcept

Definition at line 1514 of file qhash.h.

◆ operator=()

template<typename Key, typename T>
QHash & QHash< Key, T >::operator= ( const QHash< Key, T > & other)
inlinenoexcept

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

Move-assigns other to this QHash instance.

Since
5.2

Definition at line 877 of file qhash.h.

◆ operator[]() [1/4]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
T & QHash< Key, T >::operator[] ( const K & key)
inline

Definition at line 1577 of file qhash.h.

◆ operator[]() [2/4]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
const T QHash< Key, T >::operator[] ( const K & key) const
inlinenoexcept

Definition at line 1582 of file qhash.h.

◆ operator[]() [3/4]

template<typename Key, typename T>
T & QHash< Key, T >::operator[] ( const Key & key)
inline

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

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

! [qhash-iterator-invalidation-func-desc]

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. ! [qhash-iterator-invalidation-func-desc]
\sa insert(), value()

Definition at line 1110 of file qhash.h.

◆ operator[]() [4/4]

template<typename Key, typename T>
const T QHash< Key, T >::operator[] ( const Key & key) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Same as value().

Definition at line 1115 of file qhash.h.

◆ remove()

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
bool QHash< Key, T >::remove ( const K & key)
inline

Definition at line 1541 of file qhash.h.

◆ removeIf()

template<typename Key, typename T>
template<typename Predicate>
qsizetype QHash< Key, T >::removeIf ( Predicate pred)
inline
Since
6.1

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.

See also
clear(), take()

Definition at line 1013 of file qhash.h.

◆ swap()

template<typename Key, typename T>
void QHash< Key, T >::swap ( QHash< Key, T > & other)
inlinenoexcept
Since
4.8 \memberswap{hash}

Definition at line 920 of file qhash.h.

◆ take() [1/2]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
T QHash< Key, T >::take ( const K & key)
inline

Definition at line 1546 of file qhash.h.

◆ take() [2/2]

template<typename Key, typename T>
T QHash< Key, T >::take ( const Key & key)
inline

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 \l{default-constructed value}.

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

See also
remove()

Definition at line 1018 of file qhash.h.

◆ try_emplace() [1/6]

template<typename Key, typename T>
template<typename... Args>
std::pair< key_value_iterator, bool > QHash< Key, T >::try_emplace ( const Key & key,
Args &&... args )
inline

Definition at line 1417 of file qhash.h.

◆ try_emplace() [2/6]

template<typename Key, typename T>
template<typename... Args>
key_value_iterator QHash< Key, T >::try_emplace ( const_iterator hint,
const Key & key,
Args &&... args )
inline

Definition at line 1427 of file qhash.h.

◆ try_emplace() [3/6]

template<typename Key, typename T>
template<typename K, typename... Args, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
key_value_iterator QHash< Key, T >::try_emplace ( const_iterator hint,
K && key,
Args &&... args )
inline

Definition at line 1629 of file qhash.h.

◆ try_emplace() [4/6]

template<typename Key, typename T>
template<typename... Args>
key_value_iterator QHash< Key, T >::try_emplace ( const_iterator hint,
Key && key,
Args &&... args )
inline
Since
6.9

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.

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

Definition at line 1432 of file qhash.h.

◆ try_emplace() [5/6]

template<typename Key, typename T>
template<typename K, typename... Args, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
std::pair< key_value_iterator, bool > QHash< Key, T >::try_emplace ( K && key,
Args &&... args )
inline
Since
6.9

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.

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

Definition at line 1624 of file qhash.h.

◆ try_emplace() [6/6]

template<typename Key, typename T>
template<typename... Args>
std::pair< key_value_iterator, bool > QHash< Key, T >::try_emplace ( Key && key,
Args &&... args )
inline

Definition at line 1422 of file qhash.h.

◆ tryEmplace() [1/3]

template<typename Key, typename T>
template<typename... Args>
TryEmplaceResult QHash< Key, T >::tryEmplace ( const Key & key,
Args &&... args )
inline

\variable QHash::TryEmplaceResult::iterator

Holds the iterator to the newly inserted element, or the element that prevented the insertion.

\variable QHash::TryEmplaceResult::inserted

This value is {false} if there was already an entry with the same key.

Definition at line 1401 of file qhash.h.

◆ tryEmplace() [2/3]

template<typename Key, typename T>
template<typename K, typename... Args, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
TryEmplaceResult QHash< Key, T >::tryEmplace ( K && key,
Args &&... args )
inline
Since
6.9

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 \l{TryEmplaceResult}, a structure that holds an \l{QHash::TryEmplaceResult::}{iterator} to the newly created item, or to the pre-existing item that prevented the insertion, and a boolean, \l{QHash::TryEmplaceResult::}{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:

// [...]
int myKey = getKey();
qsizetype oldSize = hash.size();
MyType &elem = hash[myKey];
if (oldSize != hash.size()) // Size changed: new element!
initialize(elem);
// [use elem...]
QHash() noexcept=default
Constructs an empty hash.
QHash< int, QWidget * > hash
[35multi]
static bool initialize()
Definition qctf.cpp:95
ptrdiff_t qsizetype
Definition qtypes.h:183
QDBusVariant MyType

You can instead write:

// [...]
int myKey = getKey();
auto result = hash.tryEmplace(myKey);
if (result.inserted) // New element!
initialize(*result.iterator);
// [use result.iterator...]
GLuint64EXT * result
[6]
See also
emplace(), tryInsert(), insertOrAssign()

Definition at line 1614 of file qhash.h.

◆ tryEmplace() [3/3]

template<typename Key, typename T>
template<typename... Args>
TryEmplaceResult QHash< Key, T >::tryEmplace ( Key && key,
Args &&... args )
inline

Definition at line 1406 of file qhash.h.

◆ tryInsert() [1/2]

template<typename Key, typename T>
TryEmplaceResult QHash< Key, T >::tryInsert ( const Key & key,
const T & value )
inline

Definition at line 1411 of file qhash.h.

◆ tryInsert() [2/2]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true, if_key_constructible_from< K > = true>
TryEmplaceResult QHash< Key, T >::tryInsert ( K && key,
const T & value )
inline
Since
6.9

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 \l{TryEmplaceResult}, a structure that holds an \l{QHash::TryEmplaceResult::}{iterator} to the newly created item, or to the pre-existing item that prevented the insertion, and a boolean, \l{QHash::TryEmplaceResult::}{inserted}, denoting whether the insertion took place.

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

Definition at line 1619 of file qhash.h.

◆ value() [1/4]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
T QHash< Key, T >::value ( const K & key) const
inlinenoexcept

Definition at line 1561 of file qhash.h.

◆ value() [2/4]

template<typename Key, typename T>
template<typename K, if_heterogeneously_searchable< K > = true>
T QHash< Key, T >::value ( const K & key,
const T & defaultValue ) const
inlinenoexcept

Definition at line 1569 of file qhash.h.

◆ value() [3/4]

template<typename Key, typename T>
T QHash< Key, T >::value ( const Key & key) const
inlinenoexcept

Definition at line 1094 of file qhash.h.

◆ value() [4/4]

template<typename Key, typename T>
T QHash< Key, T >::value ( const Key & key,
const T & defaultValue ) const
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns the value associated with the key.

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

Definition at line 1102 of file qhash.h.

◆ values()

template<typename Key, typename T>
QList< T > QHash< Key, T >::values ( ) const
inline

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 \l {linear time}. The time and memory use that entails can be avoided by iterating from \l keyValueBegin() to \l keyValueEnd().

See also
keys(), value()

Definition at line 1132 of file qhash.h.

Friends And Related Symbol Documentation

◆ comparesEqual

template<typename Key, typename T>
template<typename AKey = Key, typename AT = T, QTypeTraits::compare_eq_result_container< QHash, AKey, AT > = true>
bool comparesEqual ( const QHash< Key, T > & lhs,
const QHash< Key, T > & rhs )
friend

Definition at line 933 of file qhash.h.

◆ const_iterator

template<typename Key, typename T>
friend class const_iterator
friend

Definition at line 1214 of file qhash.h.

◆ erase_if()

template<typename Key, typename T>
template< typename Key, typename T, typename Predicate > qsizetype erase_if ( QHash< Key, T > & hash,
Predicate pred )
related
Since
6.1

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.

Definition at line 2738 of file qhash.h.

◆ iterator

template<typename Key, typename T>
friend class iterator
friend

Definition at line 1174 of file qhash.h.

◆ operator<<()

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

Writes the hash hash to stream out.

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

See also
{Serializing Qt Data Types}

Definition at line 582 of file qdatastream.h.

◆ operator>>()

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

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}

Definition at line 582 of file qdatastream.h.

◆ qHash()

template<typename Key, typename T>
size_t qHash ( const QGeoCoordinate & coordinate,
size_t seed = 0 )
related

Returns a hash value for coordinate, using seed to seed the calculation.

◆ qHashBits()

template<typename Key, typename T>
size_t qHashBits ( const void * p,
size_t len,
size_t seed = 0 )
related
Since
5.4

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

◆ qHashEquals()

template<typename Key, typename T>
template< typename T > bool qHashEquals ( const T & a,
const T & b )
related
Since
6.0

This method is being used by QHash to compare two keys. Returns true if the keys a and b are considered equal for hashing purposes.

The default implementation returns the result of (a == b). It can be reimplemented for a certain type if the equality operator is not suitable for hashing purposes. This is for example the case if the equality operator uses qFuzzyCompare to compare floating point values.

Definition at line 287 of file qhashfunctions.h.

◆ qHashMulti()

template<typename Key, typename T>
template< typename... T > size_t qHashMulti ( size_t seed,
const T &... args )
related
Since
6.0

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);
...
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
See also
qHashMultiCommutative, qHashRange

Definition at line 352 of file qhashfunctions.h.

◆ qHashMultiCommutative()

template<typename Key, typename T>
template< typename... T > size_t qHashMultiCommutative ( size_t seed,
const T &... args )
related
Since
6.0

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.

See also
qHashMulti, qHashRange

Definition at line 366 of file qhashfunctions.h.

◆ qHashRange()

template<typename Key, typename T>
template< typename InputIterator > size_t qHashRange ( InputIterator first,
InputIterator last,
size_t seed = 0 )
related
Since
5.5

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.

See also
qHashBits(), qHashRangeCommutative()

Definition at line 374 of file qhashfunctions.h.

◆ qHashRangeCommutative()

template<typename Key, typename T>
template< typename InputIterator > size_t qHashRangeCommutative ( InputIterator first,
InputIterator last,
size_t seed = 0 )
related
Since
5.5

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.
See also
qHashBits(), qHashRange()

Definition at line 381 of file qhashfunctions.h.

◆ QMultiHash< Key, T >

template<typename Key, typename T>
friend class QMultiHash< Key, T >
friend

Definition at line 822 of file qhash.h.

◆ QSet< Key >

template<typename Key, typename T>
friend class QSet< Key >
friend

Definition at line 822 of file qhash.h.


The documentation for this class was generated from the following files: