本页内容

QHash::const_iterator Class

class QHash::const_iterator

QHash::const_iterator 类为QHash 提供了STL风格的const迭代器。更多内容...

公共函数

const_iterator()
const_iterator(const QHash<Key, T>::iterator &other)
const Key &key() const
const T &value() const
bool operator!=(const QHash<Key, T>::const_iterator &other) const
const T &operator*() const
QHash<Key, T>::const_iterator &operator++()
QHash<Key, T>::const_iterator operator++(int)
const T *operator->() const
bool operator==(const QHash<Key, T>::const_iterator &other) const

详细说明

QHash<Key, T>::const_iterator 允许您遍历一个QHash 。如果您希望在遍历QHash 时对其进行修改,则必须改用QHash::iterator 。 通常,对非 constQHash 也应使用QHash::const_iterator ,除非您需要通过迭代器更改QHash 。const 迭代器速度稍快,且能提高代码可读性。

默认的QHash::const_iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,必须使用QHash 函数(如QHash::cbegin()、QHash::cend() 或QHash::constFind())对其进行初始化。以下是一个典型的循环示例,用于打印哈希中存储的所有 (key, value) 对:

    QHash<QString, int> hash;
    hash.insert("January", 1);
    hash.insert("February", 2);
    //...
    hash.insert("December", 12);

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

与按键排序的QMap 不同,QHash 会以任意顺序存储其项。唯一能保证的是,具有相同键的项(因为它们是通过QMultiHash 插入的)将按从最近插入到最早插入的顺序连续出现。

同一个哈希表上可以使用多个迭代器。但请注意,直接对QHash 进行的任何修改(插入或删除项目)都可能导致迭代器失效。

向哈希表中插入项或调用诸如QHash::reserve() 或QHash::squeeze() 之类的方法,可能会使指向该哈希表的所有迭代器失效。 只有在QHash 无需扩展或缩减其内部哈希表的情况下,迭代器才保证保持有效。在发生重新哈希操作后使用任何迭代器都将导致未定义行为。

不过,您可以安全地使用迭代器通过QHash::erase() 方法从哈希表中删除条目。该函数可在迭代过程中安全调用,且不会影响哈希表中项的顺序。

警告: 隐式共享容器上的迭代器其 工作方式与 STL 迭代器并不完全相同。当迭代器在容器上处于活动状态时,应避免复制该容器。有关更多信息,请参阅《隐式共享迭代器问题》。

另请参阅 QHash::iterator 、QHash::key_iterator 以及QHash::const_key_value_iterator 。

成员函数文档

[constexpr noexcept default] const_iterator::const_iterator()

构建一个未初始化的迭代器。

不得对未初始化的迭代器调用诸如key()、value() 和 operator++() 之类的函数。在使用该迭代器之前,请使用 operator=() 向其赋值。

另请参阅 QHash::constBegin() 和QHash::constEnd()。

[noexcept] const_iterator::const_iterator(const QHash<Key, T>::iterator &other)

创建other 的副本。

[noexcept] const Key &const_iterator::key() const

返回当前项的键。

另请参阅 ` value()`。

[noexcept] const T &const_iterator::value() const

返回当前项的值。

另请参阅 key() 和operator*()。

[noexcept] bool const_iterator::operator!=(const QHash<Key, T>::const_iterator &other) const

如果 `other ` 指向的项与该迭代器指向的项不同,则返回 `true `;否则返回 `false`。

另请参阅 operator==()。

[noexcept] const T &const_iterator::operator*() const

返回当前项的值。

与value() 相同。

另请参阅 key()。

[noexcept] QHash<Key, T>::const_iterator &const_iterator::operator++()

前缀 ++ 运算符(++i )将迭代器向前移动到哈希表中的下一个项,并返回指向新当前项的迭代器。

在 `QHash::end()` 上调用此函数将导致未定义的结果。

[noexcept] QHash<Key, T>::const_iterator const_iterator::operator++(int)

后缀 ++ 运算符(i++ )将迭代器向前移动到哈希表中的下一个元素,并返回指向前一个当前元素的迭代器。

这是一个重载函数。

[noexcept] const T *const_iterator::operator->() const

返回指向当前项目值的指针。

另请参阅 value()。

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

如果 `other ` 指向与该迭代器相同的项,则返回 `true `;否则返回 `false`。

另请参阅 operator!=()。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.