QMap::const_iterator Class
class QMap::const_iteratorQMap::const_iterator 类为QMap 提供了STL风格的const迭代器。更多内容...
公共类型
公共函数
| const_iterator() | |
| const_iterator(const QMap<Key, T>::iterator &other) | |
| const Key & | key() const |
| const T & | value() const |
| const T & | operator*() const |
| QMap<Key, T>::const_iterator & | operator++() |
| QMap<Key, T>::const_iterator | operator++(int) |
| QMap<Key, T>::const_iterator & | operator--() |
| QMap<Key, T>::const_iterator | operator--(int) |
| const T * | operator->() const |
相关的非成员
| bool | operator!=(const QMap<Key, T>::const_iterator &lhs, const QMap<Key, T>::const_iterator &rhs) |
| bool | operator==(const QMap<Key, T>::const_iterator &lhs, const QMap<Key, T>::const_iterator &rhs) |
详细说明
QMap<Key, T>::const_iterator 允许您遍历QMap 。如果您希望在遍历QMap 时对其进行修改,则必须改用QMap::iterator 。 通常,对非 const 的QMap 也应使用QMap::const_iterator ,除非您需要通过迭代器修改QMap 。const 迭代器速度稍快,且能提高代码可读性。
默认的QMap::const_iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,必须使用QMap 函数(如QMap::cbegin()、QMap::cend()或QMap::constFind())对其进行初始化。以下是一个典型的循环示例,用于打印地图中存储的所有(key, value)对:
QMap<QString, int> map;
map.insert("January", 1);
map.insert("February", 2);
//...
map.insert("December", 12);
for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
cout << qPrintable(i.key()) << ": " << i.value() << endl;以下是一个示例,用于移除所有值大于 10 的项:
QMap<QString, int>::const_iterator i = map.cbegin();
while (i != map.cend()) {
if (i.value() > 10)
i = map.erase(i);
else
++i;
}以下是使用erase_if() 实现相同功能的示例:
与QHash 不同,后者以任意顺序存储项,而QMap 则按键的顺序存储项。
同一个映射上可以使用多个迭代器。如果向映射中添加项目,现有的迭代器仍然有效。如果从映射中删除项目,指向已删除项目的迭代器将变成悬空迭代器。
警告: 隐式共享容器上的迭代器其 行为与 STL 迭代器并不完全一致。应避免在迭代器处于活动状态时复制该容器。更多信息请参阅《隐式共享迭代器问题》。
另请参阅 QMap::iterator 、QMap::key_iterator 以及QMap::const_key_value_iterator 。
成员类型文档
[alias] const_iterator::iterator_category
std::bidirectional_iterator_tag的同义词,用于指示该迭代器是一个双向迭代器。
成员函数文档
[default] const_iterator::const_iterator()
构建一个未初始化的迭代器。
不得对未初始化的迭代器调用诸如key()、value() 和 operator++() 之类的函数。在使用该迭代器之前,请使用 operator=() 向其赋值。
另请参见 QMap::constBegin() 和QMap::constEnd()。
const_iterator::const_iterator(const QMap<Key, T>::iterator &other)
创建other 的副本。
const Key &const_iterator::key() const
返回当前项的键。
另请参阅 value()。
const T &const_iterator::value() const
返回当前项的值。
const T &const_iterator::operator*() const
返回当前项的值。
与value()相同。
另请参阅 key()。
QMap<Key, T>::const_iterator &const_iterator::operator++()
前缀++ 运算符(++i )将迭代器向前移动到映射中的下一个项,并返回指向新当前项的迭代器。
在 `QMap::end()` 上调用此函数将导致未定义的结果。
另请参阅 operator--()。
QMap<Key, T>::const_iterator const_iterator::operator++(int)
后缀运算符++ (i++ )将迭代器向前移动到映射中的下一个元素,并返回指向前一个当前元素的迭代器。
这是一个重载函数。
QMap<Key, T>::const_iterator &const_iterator::operator--()
前缀-- 运算符(--i )将前一个元素设为当前元素,并返回一个指向新当前元素的迭代器。
在 `QMap::begin()` 上调用此函数将导致未定义的结果。
另请参阅 operator++()。
QMap<Key, T>::const_iterator const_iterator::operator--(int)
后缀运算符-- (i-- )将前一个项设为当前项,并返回一个指向先前当前项的迭代器。
这是一个重载函数。
const T *const_iterator::operator->() const
返回指向当前项目值的指针。
另请参阅 value()。
相关的非成员
bool operator!=(const QMap<Key, T>::const_iterator &lhs, const QMap<Key, T>::const_iterator &rhs)
如果 `lhs ` 指向的对象与 `rhs ` 迭代器指向的对象不同,则返回 `true `;否则返回 `false`。
另请参阅 operator==()。
bool operator==(const QMap<Key, T>::const_iterator &lhs, const QMap<Key, T>::const_iterator &rhs)
如果 `lhs ` 指向与迭代器 `rhs ` 相同的项,则返回 `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.