QMultiHash::const_iterator Class

class QMultiHash::const_iterator

QMultiHash::const_iterator 类为QMultiHash 提供了 STL 风格的常量迭代器。更多

公共函数

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

详细描述

QMultiHash<Key,T>::const_iterator(常量迭代器)允许对QMultiHash 进行迭代。如果想在迭代过程中修改QMultiHash ,则必须使用QMultiHash::iterator 。一般来说,除非需要通过迭代器修改QMultiHash ,否则在非常数QMultiHash 上使用QMultiHash::const_iterator 也是不错的做法。常量迭代器速度稍快,而且可以提高代码的可读性。

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

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 插入的)将从最近插入的值到最晚插入的值连续出现。

可以在同一个哈希值上使用多个迭代器。但要注意,直接在QMultiHash 上执行的任何修改(插入和删除项)都会导致迭代器失效。

向散列中插入项目或调用 QMultiHash::reserve() 或 QMultiHash::squeeze() 等方法会使指向散列的所有迭代器失效。只有在QMultiHash 不必增长/收缩内部哈希表的情况下,迭代器才能保证保持有效。在发生重洗操作后使用任何迭代器都会导致未定义的行为。

如果需要长时间保留迭代器,建议使用QMultiMap 而不是QMultiHash

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

另请参见 QMultiHash::iterator,QMultiHash::key_iterator, 和QMultiHash::const_key_value_iterator

成员函数文档

[constexpr noexcept] const_iterator::const_iterator()

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

不得在未初始化的迭代器上调用key(),value() 和 operator++() 等函数。在使用迭代器之前,请使用 operator=() 为其赋值。

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

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

构造other 的副本。

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

返回当前项目的密钥。

另请参阅 value()。

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

返回当前项目的值。

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

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

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

另请参阅 operator==() 。

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

返回当前项目的值。

value() 相同。

另请参阅 key()。

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

前缀 ++ 操作符 (++i) 将迭代器前进到散列中的下一个项目,并返回一个指向新的当前项目的迭代器。

QMultiHash::end() 上调用此函数会导致未定义的结果。

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

这是一个重载函数。

后缀 ++ 运算符 (i++) 将迭代器前进到散列中的下一个项目,并返回一个指向先前当前项目的迭代器。

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

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

另请参阅 value()。

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

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

另请参阅 operator!=() 。

© 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.