QHash Class

QHash 是一种模板类, 提供基于哈希表的字典结构. 更多...

头文件: #include <QHash>
qmake: QT += core
派生类:

QMultiHash

Note: All functions in this class are reentrant.

公有类型

class const_iterator
class iterator
class key_iterator
typedef ConstIterator
typedef Iterator
typedef difference_type
typedef key_type
typedef mapped_type
typedef size_type

公有函数

QHash()
QHash(std::initializer_list<std::pair<Key, T> > list)
QHash(const QHash &other)
QHash(QHash &&other)
~QHash()
iterator begin()
const_iterator begin() const
int capacity() const
const_iterator cbegin() const
const_iterator cend() const
void clear()
const_iterator constBegin() const
const_iterator constEnd() const
const_iterator constFind(const Key &key) const
bool contains(const Key &key) const
int count(const Key &key) const
int count() const
bool empty() const
iterator end()
const_iterator end() const
QPair<iterator, iterator> equal_range(const Key &key)
QPair<const_iterator, const_iterator> equal_range(const Key &key) const
iterator erase(const_iterator pos)
iterator erase(iterator pos)
iterator find(const Key &key)
const_iterator find(const Key &key) const
iterator insert(const Key &key, const T &value)
iterator insertMulti(const Key &key, const T &value)
bool isEmpty() const
const Key key(const T &value) const
const Key key(const T &value, const Key &defaultKey) const
key_iterator keyBegin() const
key_iterator keyEnd() const
QList<Key> keys() const
QList<Key> keys(const T &value) const
int remove(const Key &key)
void reserve(int size)
int size() const
void squeeze()
void swap(QHash &other)
T take(const Key &key)
QList<Key> uniqueKeys() const
QHash &unite(const QHash &other)
const T value(const Key &key) const
const T value(const Key &key, const T &defaultValue) const
QList<T> values() const
QList<T> values(const Key &key) const
bool operator!=(const QHash &other) const
QHash &operator=(const QHash &other)
QHash &operator=(QHash &&other)
bool operator==(const QHash &other) const
T &operator[](const Key &key)
const T operator[](const Key &key) const
int qGlobalQHashSeed()
uint qHash(const QUrl &url, uint seed = 0)
uint qHash(const QDateTime &key, uint seed = 0)
uint qHash(const QDate &key, uint seed = 0)
uint qHash(const QTime &key, uint seed = 0)
uint qHash(const QPair<T1, T2> &key, uint seed = 0)
uint qHash(const std::pair<T1, T2> &key, uint seed = 0)
uint qHash(char key, uint seed = 0)
uint qHash(uchar key, uint seed = 0)
uint qHash(signed char key, uint seed = 0)
uint qHash(ushort key, uint seed = 0)
uint qHash(short key, uint seed = 0)
uint qHash(uint key, uint seed = 0)
uint qHash(int key, uint seed = 0)
uint qHash(ulong key, uint seed = 0)
uint qHash(long key, uint seed = 0)
uint qHash(quint64 key, uint seed = 0)
uint qHash(qint64 key, uint seed = 0)
uint qHash(float key, uint seed = 0)
uint qHash(double key, uint seed = 0)
uint qHash(const QChar key, uint seed = 0)
uint qHash(const QByteArray &key, uint seed = 0)
uint qHash(const QBitArray &key, uint seed = 0)
uint qHash(const QString &key, uint seed = 0)
uint qHash(const QStringRef &key, uint seed = 0)
uint qHash(QLatin1String key, uint seed = 0)
uint qHash(const T *key, uint seed = 0)
uint qHash(const QHash<Key, T> &key, uint seed = 0)
uint qHash(const QSet<T> &key, uint seed = 0)
uint qHash(const QVersionNumber &key, uint seed = 0)
qHash(QSslEllipticCurve curve, uint seed)
qHash(const QSslCertificate &key, uint seed)
qHash(const QSslError &key, uint seed)
uint qHashBits(const void *p, size_t len, uint seed = 0)
uint qHashRange(InputIterator first, InputIterator last, uint seed = 0)
uint qHashRangeCommutative(InputIterator first, InputIterator last, uint seed = 0)
void qSetGlobalQHashSeed(int newSeed)
QDataStream &operator<<(QDataStream &out, const QHash<Key, T> &hash)
QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash)

详细描述

QHash 是一种模板类, 提供基于哈希表的字典结构.

QHash<Key, T> 是一种 Qt 泛型 容器类. 它存储键值对, 提供对键关联的值的快速查找.

QHash 的功能与 QMap 非常相似. 二者的不同之处是:

  • QHash 的查询速度高于 QMap. (详见 算法复杂的.)
  • 遍历 QMap 时, 容器元素总是按照键的顺序排序. 而 QHash的元素是无序的.
  • QMap 的键类型必须提供 operator<() 运算符. QHash 的键类型必须提供 operator==() 运算符, 以及一个全局的 qHash() 函数(参见 qHash).

以下示例是一个键类型为 QString, 值类型为 intQHash:


  QHash<QString, int> hash;

你可以调用 operator[]() 运算符在哈希表中插入元素:


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

上述代码将3个元素插入到 QHash: ("one", 1), ("three", 3), 和 ("seven", 7). 另一个向哈希表插入元素的方法是调用 insert() 函数:


  hash.insert("twelve", 12);

使用 operator[]() 运算符或 value(), 查找值:


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

如果哈希表中不存在指定的键, 这些函数返回一个 默认构造的值.

如果你想检查哈希表中是否包含特定键, 使用 contains():


  int timeout = 30;
  if (hash.contains("TIMEOUT"))
      timeout = hash.value("TIMEOUT");

还有一个 value() 的重载函数, 如果哈希表中不存在指定键的元素, 该函数使用第2个参数作为默认值:


  int timeout = hash.value("TIMEOUT", 30);

一般情况下, 在哈希表中查找值时, 我们推荐你使用 contains() 和 value(), 而不是 operator[](). 原因是如果哈希表中不存在相同键的元素, operator[]() 运算符会默默地将一个元素插入到哈希表中 (除非哈希表是 const). 例如, 下面的代码片段将在内存中创建 1000 个元素:


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

为了避免这个问题, 将上面代码中的 hash[i] 替换为 hash.value(i).

QHash 内部使用哈希表执行查找. QHash的哈希表自动增长和收缩以保证在不浪费太多内存的情况下快速查找. 如果你已经知道哈希表的元素数量, 你可以调用 reserve(), 但是不能保证一定获得更好的性能. 你也可以调用 capacity() 获取哈希表的大小.

如果你想遍历 QHash 中存储的所有键值对, 你可以使用迭代器. QHash 同时提供 Java 风格迭代器 (QHashIteratorQMutableHashIterator) 和 STL 风格迭代器 (QHash::const_iteratorQHash::iterator). 下面是使用 Java 风格迭代器遍历 QHash<QString, int> :


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

下面是相同的代码, 不过这次使用 STL 风格迭代器:


  QHash<QString, int>::const_iterator i = hash.constBegin();
  while (i != hash.constEnd()) {
      cout << i.key() << ": " << i.value() << endl;
      ++i;
  }

QHash 是无序的, 所以迭代器的顺序是不可预测的. 如果要求键排序, 使用 QMap.

通常, QHash 每个键仅允许有一个值. 如果你用已存在的键调用 insert(), 先前的值将被删除. 例如:


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

但是, 你可以调用 insertMulti() instead of insert() (或使用便捷派生类 QMultiHash)为一个键存储多个值. 如果你想检索单个键的所有值, 你可以使用 values(const Key &key), 这个函数返回一个 QList<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 find() 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;
  }

如果你只想从哈希表中获取值 (而不是键), 你也可以使用 foreach:


  QHash<QString, int> hash;
  ...
  foreach (int value, hash)
      cout << value << endl;

QHash 有多种方式移除元素. 一种是调用 remove(); 这个函数移除指定键的所有元素. 另一种方式是调用 QMutableHashIterator::remove(). 另外, 你还可以调用 clear() 清除整个哈希表.

QHash的键值类型必须是 可赋值数据类型. 你不能以 QWidget 作为值; 但是, 你可以存储 QWidget *.

qHash() 哈希函数

QHash的键类型除必须是可赋值数据类型外, 还必须提供 operator==() 运算符, 并且在键类型的命名空间内还必须有一个为键类型参数返回哈希值的 qHash() 函数.

qHash() 函数基于键计算数值. 它可以使用任何可以想到的算法计算, 只要保证相同参数返回相同值就可以. 也就是说, 如果 e1 == e2, 那么 qHash(e1) == qHash(e2) 也保持成立. 然而, 为了获得更好的性能, qHash() 函数应该尽最大可能对不同的键返回不同的哈希值.

对于键类型 K, qHash 函数必须是下面两种签名之一:


  uint qHash(K key);
  uint qHash(const K &key);

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

两个参数的重载函数接受一个无符号整数参数, 该参数用来随机化哈希函数的计算. 这个种子由 QHash 提供, 为了阻止一种 算法复杂度攻击. 如果同时定义单参数和两个参数的重载函数, QHash 将使用后者(注意, 你可以定义两个参数的版本, 并对 seed 参数使用默认值).

下面是可以在 QHash 中作为键使用的 C++ 和 Qt 类型的不完全列表: 任何整数类型 (char, unsigned long 等.), 任何指针类型, QChar, QString, QByteArray. 对于所有这些类型, <QHash> 头文件会定义 qHash() 函数, 该函数计算合适的哈希值. 其它许多 Qt 类也会为其类型声明 qHash 重载函数; 具体请参考类文档.

如果你想使用其它类型作为键, 请确保提供 operator==() 运算符, 并实现 qHash() 函数.

例如:


  #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_H

上例中, 我们依赖 Qt 的全局 qHash(const QString &, uint) 函数取得雇员名字的哈希值, 然后将这个值与雇员的出生日期求异或, 来为同名雇员生成各自唯一的哈希值.

注意, Qt 提供的 qHash() 重载函数的实现可能在任何时候改变. 你 一定不能 依赖于这个假定, 认为不同 Qt 版本的 qHash() 函数 (对于相同的输入) 会计算出相同的结果.

算法复杂度攻击

所有哈希表都容易受到一种特殊类型的拒绝服务攻击, 攻击者预先仔细计算好一组不同的键, 用这些键在哈希表的同一个 bucket 中 (甚至具有相同哈希值) 进行散列. 攻击的目的是在数据输入表中时达到最坏情形的算法行为 (O(n) 而不是平均的 O(1), 详见 算法复杂度).

为了避免这种最坏情形的行为, 可以在 qHash() 计算哈希值时通过随机种子进行掺杂, 抵消攻击的程度. 该种子由 QHash 自动生成, 每个进程单独一个, 由 QHash 传给两个参数的 qHash() 重载函数的第 2 个参数.

QHash 的这种随机化处理默认是激活的. 尽管如此, 使用者不应该依赖于特定的 QHash 顺序, 这可能是在调试或回归测试等临时需要这种确定性行为的时候. 要想关闭随机化处理, 可以将环境变量 QT_HASH_SEED 设置为 0. 或者使用参数 0 调用 qSetGlobalQHashSeed() 函数.

参见 QHashIterator, QMutableHashIterator, QMap, QSet.

成员类型

typedef QHash::ConstIterator

Qt-style synonym for QHash::const_iterator.

typedef QHash::Iterator

Qt-style synonym for QHash::iterator.

typedef QHash::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

typedef QHash::key_type

Typedef for Key. Provided for STL compatibility.

typedef QHash::mapped_type

Typedef for T. Provided for STL compatibility.

typedef QHash::size_type

Typedef for int. Provided for STL compatibility.

成员函数

QHash::QHash()

Constructs an empty hash.

参见 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.

This function is only available if the program is being compiled in C++11 mode.

This function was introduced in Qt 5.1.

QHash::QHash(const QHash &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.

参见 operator=().

QHash::QHash(QHash &&other)

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

This function was introduced in Qt 5.2.

QHash::~QHash()

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

iterator QHash::begin()

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

参见 constBegin() and end().

const_iterator QHash::begin() const

This is an overloaded function.

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

参见 reserve() and squeeze().

const_iterator QHash::cbegin() const

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

This function was introduced in Qt 5.0.

参见 begin() and cend().

const_iterator QHash::cend() const

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

This function was introduced in Qt 5.0.

参见 cbegin() and end().

void QHash::clear()

Removes all items from the hash.

参见 remove().

const_iterator QHash::constBegin() const

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

参见 begin() and constEnd().

const_iterator QHash::constEnd() const

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

参见 constBegin() and end().

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

This function was introduced in Qt 4.1.

参见 find() and QMultiHash::constFind().

bool QHash::contains(const Key &key) const

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

参见 count() and QMultiHash::contains().

int QHash::count(const Key &key) const

Returns the number of items associated with the key.

参见 contains() and insertMulti().

int QHash::count() const

This is an overloaded function.

Same as size().

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.

iterator QHash::end()

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

参见 begin() and constEnd().

const_iterator QHash::end() const

This is an overloaded function.

QPair<iterator, 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 end().

This function was introduced in Qt 5.7.

QPair<const_iterator, const_iterator> QHash::equal_range(const Key &key) const

This is an overloaded function.

This function was introduced in Qt 5.7.

iterator QHash::erase(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 remove() and take(), 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;
      }
  }

This function was introduced in Qt 5.7.

参见 remove(), take(), and find().

iterator QHash::erase(iterator pos)

This is an overloaded function.

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;
  }

参见 value(), values(), and QMultiHash::find().

const_iterator QHash::find(const Key &key) const

This is an overloaded function.

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.

参见 insertMulti().

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 insert(), which overwrites the value of an existing item.)

参见 insert() and values().

bool QHash::isEmpty() const

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

参见 size().

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 default-constructed key.

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

参见 value() and keys().

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 (linear time), because QHash's internal data structure is optimized for fast lookup by key, not by value.

This function was introduced in Qt 4.3.

key_iterator QHash::keyBegin() const

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

This function was introduced in Qt 5.6.

参见 keyEnd().

key_iterator QHash::keyEnd() const

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

This function was introduced in Qt 5.6.

参见 keyBegin().

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 insertMulti(), or unite() 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 uniqueKeys().

The order is guaranteed to be the same as that used by values().

参见 uniqueKeys(), values(), and key().

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 (linear time), because QHash'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 insertMulti() has been used with the key.

参见 clear(), take(), and QMultiHash::remove().

void QHash::reserve(int size)

Ensures that the QHash'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 QHash will use a prime number internally anyway. 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 shrinks or grows to provide good performance without wasting too much memory.

参见 squeeze() and capacity().

int QHash::size() const

Returns the number of items in the hash.

参见 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.

参见 reserve() and capacity().

void QHash::swap(QHash &other)

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

This function was introduced in Qt 4.8.

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 there are multiple items for key in the hash, only the most recently inserted one is removed.

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

参见 remove().

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 insertMulti(), or unite() was used) occur only once in the returned list.

This function was introduced in Qt 4.2.

参见 keys() and values().

QHash &QHash::unite(const QHash &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.

参见 insertMulti().

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 default-constructed value. If there are multiple items for the key in the hash, the value of the most recently inserted one is returned.

参见 key(), values(), contains(), and operator[]().

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

参见 keys() and value().

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.

参见 count() and insertMulti().

bool QHash::operator!=(const QHash &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==().

参见 operator==().

QHash &QHash::operator=(const QHash &other)

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

QHash &QHash::operator=(QHash &&other)

Move-assigns other to this QHash instance.

This function was introduced in Qt 5.2.

bool QHash::operator==(const QHash &other) const

Returns true if other is 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==().

参见 operator!=().

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. If the hash contains multiple items with the key, this function returns a reference to the most recently inserted value.

参见 insert() and value().

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

This is an overloaded function.

Same as value().

相关非成员

int qGlobalQHashSeed()

Returns the current global QHash seed.

The seed is set in any newly created QHash. See qHash about how this seed is being used by QHash.

This function was introduced in Qt 5.6.

参见 qSetGlobalQHashSeed.

uint qHash(const QUrl &url, uint seed = 0)

Returns the hash value for the url. If specified, seed is used to initialize the hash.

This function was introduced in Qt 5.0.

uint qHash(const QDateTime &key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(const QDate &key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(const QTime &key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(const QPair<T1, T2> &key, uint seed = 0)

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

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

This function was introduced in Qt 5.0.

uint qHash(const std::pair<T1, T2> &key, uint seed = 0)

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

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

Note: The return type of this function is not the same as that of


  qHash(qMakePair(key.first, key.second), seed);

The two functions use different hashing algorithms; due to binary compatibility constraints, we cannot change the QPair algorithm to match the std::pair one before Qt 6.

This function was introduced in Qt 5.7.

uint qHash(char key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(uchar key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(signed char key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(ushort key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(short key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(uint key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(int key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(ulong key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(long key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(quint64 key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(qint64 key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(float key, uint seed = 0)

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

This function was introduced in Qt 5.3.

uint qHash(double key, uint seed = 0)

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

This function was introduced in Qt 5.3.

uint qHash(const QChar key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(const QByteArray &key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(const QBitArray &key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(const QString &key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(const QStringRef &key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(QLatin1String key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(const T *key, uint seed = 0)

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

This function was introduced in Qt 5.0.

uint qHash(const QHash<Key, T> &key, uint seed = 0)

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.

uint qHash(const QSet<T> &key, uint seed = 0)

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.

uint qHash(const QVersionNumber &key, uint seed = 0)

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

This function was introduced in Qt 5.6.

qHash(QSslEllipticCurve curve, uint seed)

This function was introduced in Qt 5.5.

qHash(const QSslCertificate &key, uint seed)

This function was introduced in Qt 5.4.

qHash(const QSslError &key, uint seed)

This function was introduced in Qt 5.4.

uint qHashBits(const void *p, size_t len, uint 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 uint qHash(const std::vector<int> &key, uint 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.

This function was introduced in Qt 5.4.

参见 qHashRange() and qHashRangeCommutative().

uint qHashRange(InputIterator first, InputIterator last, uint 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 uint qHash(const std::vector<int> &key, uint 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.

This function was introduced in Qt 5.5.

参见 qHashBits() and qHashRangeCommutative().

uint qHashRangeCommutative(InputIterator first, InputIterator last, uint 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 uint qHash(const std::unordered_set<int> &key, uint 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.

This function was introduced in Qt 5.5.

参见 qHashBits() and qHashRange().

void qSetGlobalQHashSeed(int newSeed)

Sets the global QHash seed to newSeed.

Manually setting the global QHash seed value should be done only for testing and debugging purposes, when deterministic and reproducible behavior on a QHash is needed. We discourage to do it in production code as it can make your application susceptible to algorithmic complexity attacks.

The seed is set in any newly created QHash. See qHash about how this seed is being used by QHash.

If the environment variable QT_HASH_SEED is set, calling this function will result in a no-op.

Passing the value -1 will reinitialize the global QHash seed to a random value.

This function was introduced in Qt 5.6.

参见 qGlobalQHashSeed.

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

参见 Serializing Qt Data Types.

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

参见 Serializing Qt Data Types.