本页内容

QMultiHash::const_iterator Class

class QMultiHash::const_iterator

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

公共函数

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 。 通常,对非 const 的QMultiHash 也应使用QMultiHash::const_iterator ,除非您需要通过迭代器修改QMultiHash 。const 迭代器速度稍快,且能提高代码可读性。

默认的QMultiHash::const_iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,您必须使用QMultiHash 函数(如QMultiHash::cbegin()、QMultiHash::cend()或QMultiHash::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 不同,QMultiHash 会以任意顺序存储其项。唯一的保证是:具有相同键的项(因为它们是通过QMultiHash 插入的)将按从最近插入到最早插入的顺序连续出现。

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

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

如果您需要长期保留迭代器,建议使用QMultiMap 而非QMultiHash 。

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

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

成员函数文档

[constexpr noexcept default] 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!=()。

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