QHash Class▲
-
Header: QHash
-
qmake: QT += core
-
Group: QHash is part of tools, https://
Detailed Description▲
https://<Key, T> is one of Qt's generic https://. It stores (key, value) pairs and provides very fast lookup of the value associated with a key.
https:// provides very similar functionality to https://. The differences are:
-
https:// provides faster lookups than https://. (See https:// for details.)
-
When iterating over a https://, the items are always sorted by key. With https://, the items are arbitrarily ordered.
-
The key type of a https:// must provide operator<(). The key type of a https:// must provide operator==() and a global hash function called https://() (see https://).
Here's an example https:// 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 https://: ("one", 1), ("three", 3), and ("seven", 7). Another way to insert items into the hash is to use https://():
hash.insert("twelve", 12);To look up a value, use operator[]() or https://():
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 https://.
If you want to check whether the hash contains a particular key, use https://():
int timeout = 30;
if (hash.contains("TIMEOUT"))
timeout = hash.value("TIMEOUT");There is also a https://() 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 https://() and https://() 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, https:// uses a hash table to perform lookups. This hash table automatically grows and shrinks to provide fast lookups without wasting too much memory. You can still control the size of the hash table by calling https://() if you already know approximately how many items the https:// will contain, but this isn't necessary to obtain good performance. You can also call https://() to retrieve the hash table's size.
If you want to navigate through all the (key, value) pairs stored in a https://, you can use an iterator. https:// provides both https:// (https:// and https://) and https:// (https:// and https://). Here's how to iterate over a https://<QString, int> using a Java-style iterator:
QHashIterator<QString, int> i(hash);
while (i.hasNext()) {
i.next();
cout << i.key() << ": " << i.value() << endl;
}Here's the same code, but using an STL-style iterator:
QHash<QString, int>::const_iterator i = hash.constBegin();
while (i != hash.constEnd()) {
cout << i.key() << ": " << i.value() << endl;
++i;
}https:// is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a https://.
Normally, a https:// allows only one value per key. If you call https://() with a key that already exists in the https://, the previous value is erased. For example:
hash.insert("plenty", 100);
hash.insert("plenty", 2000);
// hash.value("plenty") == 2000However, you can store multiple values per key by using https://() instead of https://() (or using the convenience subclass QMultiHash). If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a https://<T>:
QList<int> values = hash.values("plenty");
for (int i = 0; i < values.size(); ++i)
cout << values.at(i) << endl;The items that share the same key are available from most recently to least recently inserted. A more efficient approach is to call https://() to get the iterator for the first item with a key and iterate from there:
QHash<QString, int>::iterator i = hash.find("plenty");
while (i != hash.end() && i.key() == "plenty") {
cout << i.value() << endl;
++i;
}If you only need to extract the values from a hash (not the keys), you can also use https://:
QHash<QString, int> hash;
...
foreach (int value, hash)
cout << value << endl;Items can be removed from the hash in several ways. One way is to call https://(); this will remove any item with the given key. Another way is to use https://(). In addition, you can clear the entire hash using https://().
https://'s key and value data types must be https://. You cannot, for example, store a QWidget as a value; instead, store a QWidget *.
The qHash() hashing function▲
A https://'s key type has additional requirements other than being an assignable data type: it must provide operator==(), and there must also be a https://() function in the type's namespace that returns a hash value for an argument of the key's type.
The https://() 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 qHash(e1) == qHash(e2) must hold as well. However, to obtain good performance, the https://() function should attempt to return different hash values for different keys to the largest extent possible.
For a key type K, the https:// function must have one of these signatures:
uint qHash(K key);
uint qHash(const K &key);
uint qHash(K key, uint seed);
uint qHash(const K &key, uint seed);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 https:// in order to prevent a family of https://. If both a one-argument and a two-arguments overload are defined for a key type, the latter is used by https:// (note that you can simply define a two-arguments version, and use a default value for the seed parameter).
Here's a partial list of the C++ and Qt types that can serve as keys in a https://: any integer type (char, unsigned long, etc.), any pointer type, QChar, QString, and QByteArray. For all of these, the <QHash> header defines a https://() function that computes an adequate hash value. Many other Qt classes also declare a https:// 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 https://() implementation.
Example:
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
class Employee
{
public:
Employee() {}
Employee(const QString &name, const 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 uint qHash(const Employee &key, uint seed)
{
return qHash(key.name(), seed) ^ key.dateOfBirth().day();
}
#endif // EMPLOYEE_HIn the example above, we've relied on Qt's global https://(const QString &, uint) to give us a hash value for the employee's name, and XOR'ed this with the day they were born to help produce unique hashes for people with the same name.
Note that the implementation of the https://() overloads offered by Qt may change at any time. You must not rely on the fact that https://() 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 https:// 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 https://() can be salted by a random seed, that nullifies the attack's extent. This seed is automatically generated by https:// once per process, and then passed by https:// as the second argument of the two-arguments overload of the https://() function.
This randomization of https:// is enabled by default. Even though programs should never depend on a particular https:// 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 https://() function with the value 0.
See Also▲
See also https://, https://, https://, https://
Member Type Documentation▲
QHash::ConstIterator▲
Qt-style synonym for https://.
QHash::Iterator▲
Qt-style synonym for https://.
[since 5.10] QHash::const_key_value_iterator▲
The https:// typedef provides an STL-style const iterator for https:// and QMultiHash.
QHash::const_key_value_iterator is essentially the same as https:// with the difference that operator*() returns a key/value pair instead of a value.
This typedef was introduced in Qt 5.10.
See Also▲
See also https://
QHash::difference_type▲
Typedef for ptrdiff_t. Provided for STL compatibility.
QHash::key_type▲
Typedef for Key. Provided for STL compatibility.
[since 5.10] QHash::key_value_iterator▲
The https:// typedef provides an STL-style iterator for https:// and QMultiHash.
QHash::key_value_iterator is essentially the same as https:// with the difference that operator*() returns a key/value pair instead of a value.
This typedef was introduced in Qt 5.10.
See Also▲
See also https://
QHash::mapped_type▲
Typedef for T. Provided for STL compatibility.
QHash::size_type▲
Typedef for int. Provided for STL compatibility.
Member Function Documentation▲
QHash::QHash()▲
[since 5.2] QHash::QHash(QHash<Key, T> &&other)▲
Move-constructs a https:// instance, making it point at the same object that other was pointing to.
This function was introduced in Qt 5.2.
[default, since 5.1] 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.
This function is only available if the program is being compiled in C++11 mode.
This function was introduced in Qt 5.1.
[default] QHash::QHash(const QHash<Key, T> &other)▲
Constructs a copy of other.
This operation occurs in https://, because https:// is https://. This makes returning a https:// from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takes https://.
See Also▲
See also https://()
[default] QHash::~QHash()▲
Destroys the hash. References to the values in the hash and all iterators of this hash become invalid.
QHash::iterator QHash::begin()▲
Returns an https:// pointing to the first item in the hash.
See Also▲
See also https://(), https://()
QHash::const_iterator QHash::begin() const▲
This is an overloaded function.
int QHash::capacity() const▲
Returns the number of buckets in the https://'s internal hash table.
The sole purpose of this function is to provide a means of fine tuning https://'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 https://().
See Also▲
See also https://(), https://()
[since 5.0] QHash::const_iterator QHash::cbegin() const▲
Returns a const https:// pointing to the first item in the hash.
This function was introduced in Qt 5.0.
See Also▲
See also https://(), https://()
[since 5.0] QHash::const_iterator QHash::cend() const▲
Returns a const https:// pointing to the imaginary item after the last item in the hash.
This function was introduced in Qt 5.0.
See Also▲
See also https://(), https://()
void QHash::clear()▲
QHash::const_iterator QHash::constBegin() const▲
Returns a const https:// pointing to the first item in the hash.
See Also▲
See also https://(), https://()
QHash::const_iterator QHash::constEnd() const▲
Returns a const https:// pointing to the imaginary item after the last item in the hash.
See Also▲
See also https://(), https://()
[since 4.1] QHash::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 https://().
This function was introduced in Qt 4.1.
See Also▲
See also https://(), QMultiHash::constFind()
[since 5.10] QHash::const_key_value_iterator QHash::constKeyValueBegin() const▲
Returns a const https:// pointing to the first entry in the hash.
This function was introduced in Qt 5.10.
See Also▲
See also https://()
[since 5.10] QHash::const_key_value_iterator QHash::constKeyValueEnd() const▲
Returns a const https:// pointing to the imaginary entry after the last entry in the hash.
This function was introduced in Qt 5.10.
See Also▲
See also https://()
bool QHash::contains(const Key &key) const▲
Returns true if the hash contains an item with the key; otherwise returns false.
See Also▲
See also https://(), QMultiHash::contains()
int QHash::count(const Key &key) const▲
int QHash::count() const▲
This is an overloaded function.
Same as https://().
bool QHash::empty() const▲
This function is provided for STL compatibility. It is equivalent to https://(), returning true if the hash is empty; otherwise returns false.
QHash::iterator QHash::end()▲
Returns an https:// pointing to the imaginary item after the last item in the hash.
See Also▲
See also https://(), https://()
QHash::const_iterator QHash::end() const▲
This is an overloaded function.
[since 5.7] QPair<QHash::iterator, QHash::iterator> QHash::equal_range(const Key &key)▲
Returns a pair of iterators delimiting the range of values [first, second), that are stored under key. If the range is empty then both iterators will be equal to https://().
This function was introduced in Qt 5.7.
[since 5.7] QPair<QHash::const_iterator, QHash::const_iterator> QHash::equal_range(const Key &key) const▲
This is an overloaded function.
This function was introduced in Qt 5.7.
[since 5.7] QHash::iterator QHash::erase(QHash::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.
Unlike https://() and https://(), this function never causes https:// 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;
}
}This function was introduced in Qt 5.7.
See Also▲
See also https://(), https://(), https://()
QHash::iterator QHash::erase(QHash::iterator pos)▲
This is an overloaded function.
QHash::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 https://().
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;
}See Also▲
See also https://(), https://(), QMultiHash::find()
QHash::const_iterator QHash::find(const Key &key) const▲
This is an overloaded function.
QHash::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.
If there are multiple items with the key, the most recently inserted item's value is replaced with value.
See Also▲
See also https://()
QHash::iterator QHash::insertMulti(const Key &key, const T &value)▲
Inserts a new item with the key and a value of value.
If there is already an item with the same key in the hash, this function will simply create a new one. (This behavior is different from https://(), which overwrites the value of an existing item.)
See Also▲
See also https://(), https://()
bool QHash::isEmpty() const▲
const Key QHash::key(const T &value) const▲
Returns the first key mapped to value.
If the hash contains no item with the value, the function returns a https://.
This function can be slow (https://), because https://'s internal data structure is optimized for fast lookup by key, not by value.
See Also▲
See also https://(), https://()
[since 4.3] const Key QHash::key(const T &value, const Key &defaultKey) const▲
This is an overloaded function.
Returns the first key mapped to value, or defaultKey if the hash contains no item mapped to value.
This function can be slow (https://), because https://'s internal data structure is optimized for fast lookup by key, not by value.
This function was introduced in Qt 4.3.
[since 5.6] QHash::key_iterator QHash::keyBegin() const▲
Returns a const https:// pointing to the first key in the hash.
This function was introduced in Qt 5.6.
See Also▲
See also https://()
[since 5.6] QHash::key_iterator QHash::keyEnd() const▲
Returns a const https:// pointing to the imaginary item after the last key in the hash.
This function was introduced in Qt 5.6.
See Also▲
See also https://()
[since 5.10] QHash::key_value_iterator QHash::keyValueBegin()▲
Returns an https:// pointing to the first entry in the hash.
This function was introduced in Qt 5.10.
See Also▲
See also https://()
[since 5.10] QHash::const_key_value_iterator QHash::keyValueBegin() const▲
Returns a const https:// pointing to the first entry in the hash.
This function was introduced in Qt 5.10.
See Also▲
See also https://()
[since 5.10] QHash::key_value_iterator QHash::keyValueEnd()▲
Returns an https:// pointing to the imaginary entry after the last entry in the hash.
This function was introduced in Qt 5.10.
See Also▲
See also https://()
[since 5.10] QHash::const_key_value_iterator QHash::keyValueEnd() const▲
Returns a const https:// pointing to the imaginary entry after the last entry in the hash.
This function was introduced in Qt 5.10.
See Also▲
See also https://()
QList<Key> QHash::keys() const▲
Returns a list containing all the keys in the hash, in an arbitrary order. Keys that occur multiple times in the hash (because items were inserted with https://(), or https://() was used) also occur multiple times in the list.
To obtain a list of unique keys, where each key from the map only occurs once, use https://().
The order is guaranteed to be the same as that used by https://().
See Also▲
See also https://(), https://(), https://()
QList<Key> QHash::keys(const T &value) const▲
This is an overloaded function.
Returns a list containing all the keys associated with value value, in an arbitrary order.
This function can be slow (https://), because https://'s internal data structure is optimized for fast lookup by key, not by value.
int QHash::remove(const Key &key)▲
Removes all the items that have the key from the hash. Returns the number of items removed which is usually 1 but will be 0 if the key isn't in the hash, or greater than 1 if https://() has been used with the key.
See Also▲
See also https://(), https://(), QMultiHash::remove()
void QHash::reserve(int size)▲
Ensures that the https://'s internal hash table consists of at least size buckets.
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 slightly more than the maximum number of items expected in the hash. size doesn't have to be prime, because https:// will use a prime number internally anyway. If size is an underestimate, the worst that will happen is that the https:// will be a bit slower.
In general, you will rarely ever need to call this function. https://'s internal hash table automatically shrinks or grows to provide good performance without wasting too much memory.
See Also▲
See also https://(), https://()
int QHash::size() const▲
void QHash::squeeze()▲
Reduces the size of the https://'s internal hash table to save memory.
The sole purpose of this function is to provide a means of fine tuning https://'s memory usage. In general, you will rarely ever need to call this function.
See Also▲
See also https://(), https://()
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 https://. If there are multiple items for key in the hash, only the most recently inserted one is removed.
If you don't use the return value, https://() is more efficient.
See Also▲
See also https://()
[since 4.2] QList<Key> QHash::uniqueKeys() const▲
Returns a list containing all the keys in the map. Keys that occur multiple times in the map (because items were inserted with https://(), or https://() was used) occur only once in the returned list.
This function was introduced in Qt 4.2.
See Also▲
See also https://(), https://()
QHash<Key, T> &QHash::unite(const QHash<Key, T> &other)▲
Inserts all the items in the other hash into this hash. If a key is common to both hashes, the resulting hash will contain the key multiple times.
See Also▲
See also https://()
const T QHash::value(const Key &key) const▲
Returns the value associated with the key.
If the hash contains no item with the key, the function returns a https://. If there are multiple items for the key in the hash, the value of the most recently inserted one is returned.
See Also▲
See also https://(), https://(), https://(), https://()
const T QHash::value(const Key &key, const T &defaultValue) const▲
This is an overloaded function.
If the hash contains no item with the given key, the function returns defaultValue.
QList<T> QHash::values() const▲
Returns a list containing all the values in the hash, in an arbitrary order. If a key is associated with multiple values, all of its values will be in the list, and not just the most recently inserted one.
The order is guaranteed to be the same as that used by https://().
See Also▲
See also https://(), https://()
QList<T> QHash::values(const Key &key) const▲
This is an overloaded function.
Returns a list of all the values associated with the key, from the most recently inserted to the least recently inserted.
See Also▲
See also https://(), https://()
bool QHash::operator!=(const QHash<Key, T> &other) const▲
Returns true if other is not equal to this 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▲
See also operator==()
[default] int &QHash::operator=(const QHash<Key, T> &)▲
Assigns other to this hash and returns a reference to this hash.
[default, since 5.2] int &QHash::operator=(QHash<Key, T> &&)▲
Move-assigns other to this https:// instance.
This function was introduced in Qt 5.2.
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 https:// into the hash with the key, and returns a reference to it. If the hash contains multiple items with the key, this function returns a reference to the most recently inserted value.
See Also▲
See also https://(), https://()
const T QHash::operator[](const Key &key) const▲
This is an overloaded function.
Same as https://().
Related Non-Members▲
[since 5.6] Q_DECL_EXPORT qGlobalQHashSeed()▲
Returns the current global https:// seed.
The seed is set in any newly created https://. See https:// about how this seed is being used by https://.
This function was introduced in Qt 5.6.
See Also▲
See also https://
[since 5.8] int qHash(const QHash<Key, T> &, int)▲
Returns the hash value for the key, using seed to seed the calculation.
Type T must be supported by qHash().
This function was introduced in Qt 5.8.
[since 5.5] int qHash(const QSet<T> &, int)▲
Returns the hash value for the key, using seed to seed the calculation.
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.
This function was introduced in Qt 5.5.
[since 5.6] int qHash(const QVersionNumber &, int)▲
Returns the hash value for the key, using seed to seed the calculation.
This function was introduced in Qt 5.6.
[since 5.6] Q_DECL_EXPORT qSetGlobalQHashSeed(int newSeed)▲
Sets the global https:// seed to newSeed.
Manually setting the global https:// seed value should be done only for testing and debugging purposes, when deterministic and reproducible behavior on a https:// is needed. We discourage to do it in production code as it can make your application susceptible to https://.
From Qt 5.10 and onwards, the only allowed values are 0 and -1. Passing the value -1 will reinitialize the global https:// seed to a random value, while the value of 0 is used to request a stable algorithm for C++ primitive types types (like int) and string types (QString, QByteArray).
The seed is set in any newly created https://. See https:// about how this seed is being used by https://.
If the environment variable QT_HASH_SEED is set, calling this function will result in a no-op.
This function was introduced in Qt 5.6.
See Also▲
See also https://
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▲
See also https://
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▲
See also https://


