QHash::iterator Class
class QHash::iteratorQHash::iterator 类为QHash 提供了一个STL风格的非const迭代器。更多内容...
公共函数
| iterator() | |
| const Key & | key() const |
| T & | value() const |
| bool | operator!=(const QHash<Key, T>::const_iterator &other) const |
| bool | operator!=(const QHash<Key, T>::iterator &other) const |
| T & | operator*() const |
| QHash<Key, T>::iterator & | operator++() |
| QHash<Key, T>::iterator | operator++(int) |
| T * | operator->() const |
| bool | operator==(const QHash<Key, T>::const_iterator &other) const |
| bool | operator==(const QHash<Key, T>::iterator &other) const |
详细说明
QHash<Key, T>::iterator 允许您遍历QHash ,并修改与特定键关联的值(但不能修改该键)。 如果您想遍历一个 constQHash ,则应使用QHash::const_iterator 。通常,对非 constQHash 也使用QHash::const_iterator 是一种良好的编程习惯,除非您需要通过迭代器更改QHash 。Const 迭代器速度稍快,并且可以提高代码的可读性。
默认的QHash::iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,你必须使用类似QHash::begin()、QHash::end() 或QHash::find() 的QHash 函数对其进行初始化。以下是一个典型的循环,用于打印哈希中存储的所有 (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 存储项的顺序是随机的。
以下示例将QHash 中存储的每个值加 2:
for (auto i = hash.begin(), end = hash.end(); i != end; ++i)
i.value() += 2;要从QHash 中移除元素,可以使用erase_if(QHash<Key, T> &map, Predicate pred):
同一个哈希表可以同时使用多个迭代器。但请注意,对QHash 进行的任何直接修改(如插入或删除项)都可能导致迭代器失效。
向哈希表中插入元素,或调用QHash::reserve() 或QHash::squeeze() 等方法,可能会导致所有指向该哈希表的迭代器失效。 只有在QHash 无需扩展或缩小其内部哈希表的情况下,迭代器才保证保持有效。在发生重新哈希操作后使用任何迭代器都将导致未定义的行为。
如果您需要长期保留迭代器,建议使用QMap 而不是QHash 。
警告: 隐式共享容器上的迭代器其 工作方式与 STL 迭代器并不完全相同。当迭代器在容器上处于活动状态时,应避免复制该容器。有关更多信息,请参阅《隐式共享迭代器问题》。
另请参阅 QHash::const_iterator 、QHash::key_iterator 和QHash::key_value_iterator 。
成员函数文档
[constexpr noexcept default] iterator::iterator()
构建一个未初始化的迭代器。
不得对未初始化的迭代器调用key()、value() 和 operator++() 等函数。使用前请先通过 operator=() 向其赋值。
另请参阅 QHash::begin() 和QHash::end()。
[noexcept] const Key &iterator::key() const
返回当前项的键,作为常量引用。
虽然无法直接通过迭代器更改项的键,但可以通过先调用 `QHash::erase()`,再调用 `QHash::insert()` 来实现。
另请参阅 value()。
[noexcept] T &iterator::value() const
返回指向当前项值的可修改引用。
您可以在赋值语句的左侧使用 value() 来更改项的值,例如:
if (i.key() == "Hello")
i.value() = "Bonjour";[noexcept] bool iterator::operator!=(const QHash<Key, T>::const_iterator &other) const
[noexcept] bool iterator::operator!=(const QHash<Key, T>::iterator &other) const
如果 `other ` 指向的对象与该迭代器指向的对象不同,则返回 `true `;否则返回 `false`。
另请参阅 operator==()。
[noexcept] T &iterator::operator*() const
返回指向当前项值的可修改引用。
与value()相同。
另请参阅 key()。
[noexcept] QHash<Key, T>::iterator &iterator::operator++()
前缀 ++ 运算符(++i )将迭代器向前移动到哈希表中的下一个项目,并返回指向新当前项目的迭代器。
在 `QHash::end()` 上调用此函数将导致未定义的结果。
[noexcept] QHash<Key, T>::iterator iterator::operator++(int)
后缀 ++ 运算符(i++ )将迭代器向前移动到哈希表中的下一个元素,并返回指向前一个当前元素的迭代器。
这是一个重载函数。
[noexcept] T *iterator::operator->() const
返回指向当前项目值的指针。
另请参阅 value()。
[noexcept] bool iterator::operator==(const QHash<Key, T>::const_iterator &other) const
[noexcept] bool iterator::operator==(const QHash<Key, T>::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.