QMultiHash::iterator Class
class QMultiHash::iteratorQMultiHash::iterator 类为QMultiHash 提供了 STL 风格的非常数迭代器。更多
公共函数
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 ,并修改与特定键关联的值(而不是键)。如果要遍历一个常量QMultiHash ,则应使用QMultiHash::const_iterator 。一般来说,除非需要通过迭代器修改QMultiHash ,否则在非常量QMultiHash 上使用QMultiHash::const_iterator 也是不错的做法。常量迭代器速度稍快,而且可以提高代码的可读性。
默认的QMultiHash::iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,必须使用QMultiHash 函数(如QMultiHash::begin()、QMultiHash::end() 或QMultiHash::find() )对其进行初始化。下面是一个典型的循环,用于打印散列中存储的所有(键、值)对:
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(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 。
成员函数文档
[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]
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==() 。
[constexpr noexcept]
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]
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()。
© 2025 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.