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