QMap::iterator Class
class QMap::iteratorQMap::iterator 类为QMap 提供了 STL 风格的非常数迭代器。更多
公共类型
公共函数
iterator() | |
const Key & | key() const |
T & | value() const |
T & | operator*() const |
QMap<Key, T>::iterator & | operator++() |
QMap<Key, T>::iterator | operator++(int) |
QMap<Key, T>::iterator & | operator--() |
QMap<Key, T>::iterator | operator--(int) |
T * | operator->() const |
相关非成员
bool | operator!=(const QMap<Key, T>::iterator &lhs, const QMap<Key, T>::iterator &rhs) |
bool | operator==(const QMap<Key, T>::iterator &lhs, const QMap<Key, T>::iterator &rhs) |
详细说明
QMap<Key, T>::iterator 允许您遍历QMap ,并修改存储在特定键下的值(而不是键)。如果要遍历一个常量QMap ,则应使用QMap::const_iterator 。一般来说,除非需要通过迭代器修改QMap ,否则在非常量QMap 上使用QMap::const_iterator 也是不错的做法。常量迭代器速度稍快,而且可以提高代码的可读性。
默认的QMap::iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,必须使用QMap 函数(如QMap::begin()、QMap::end() 或QMap::find() )对其进行初始化。下面是一个典型的循环,用于打印存储在 map 中的所有(键、值)对:
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;
QHash 按任意顺序存储项,而QMap 则按键顺序存储项。
下面是一个将QMap 中存储的每个值递增 2 的示例:
for (auto i = map.begin(), end = map.end(); i != end; ++i) i.value() += 2;
要从QMap 中删除元素,可以使用erase_if(QMap<Key, T> &map, Predicate pred):
可以在同一个 map 上使用多个迭代器。如果向映射中添加项目,现有的迭代器将保持有效。如果从映射中移除项目,指向被移除项目的迭代器将成为悬空迭代器。
警告: 隐式共享容器上的迭代器与 STL-迭代器的工作方式并不完全相同。当容器上的迭代器处于活动状态时,应避免复制该容器。更多信息,请阅读隐式共享迭代器问题。
另请参见 QMap::const_iterator,QMap::key_iterator, 和QMap::key_value_iterator 。
成员函数文档
iterator::iterator()
构造一个未初始化的迭代器。
不得在未初始化的迭代器上调用key(),value() 和 operator++() 等函数。在使用迭代器之前,请使用 operator=() 为其赋值。
另请参阅 QMap::begin() 和QMap::end()。
const Key &iterator::key() const
以常量引用形式返回当前项目的键值。
虽然可以通过调用QMap::erase() 后再调用QMap::insert() 来改变项的键值,但无法通过迭代器直接改变项的键值。
另请参见 value()。
T &iterator::value() const
返回当前项目值的可修改引用。
例如,您可以在赋值的左侧使用 value() 来更改项目的值:
if (i.key() == "Hello") i.value() = "Bonjour";
T &iterator::operator*() const
返回当前项目值的可修改引用。
与value() 相同。
另请参阅 key()。
QMap<Key, T>::iterator &iterator::operator++()
前缀++
操作符 (++i
) 将迭代器前进到映射中的下一个项,并返回一个指向新的当前项的迭代器。
在QMap::end() 上调用此函数会导致未定义的结果。
另请参见 operator--()。
QMap<Key, T>::iterator iterator::operator++(int)
这是一个重载函数。
后缀++
运算符 (i++
) 将迭代器前进到 map 中的下一个项目,并返回一个指向先前当前项目的迭代器。
QMap<Key, T>::iterator &iterator::operator--()
前缀--
操作符 (--i
) 使前一个项目成为当前项目,并返回一个指向新的当前项目的迭代器。
在QMap::begin() 上调用此函数会导致未定义的结果。
另请参见 operator++()。
QMap<Key, T>::iterator iterator::operator--(int)
这是一个重载函数。
后缀--
运算符 (i--
) 使前一个项目成为当前项目,并返回一个指向前一个当前项目的迭代器。
T *iterator::operator->() const
返回指向当前项目值的指针。
另请参阅 value()。
相关非成员
bool operator==(const QMap<Key, T>::iterator &lhs, const QMap<Key, T>::iterator &rhs)
如果lhs 指向与rhs 迭代器相同的项目,则返回true
;否则返回false
。
另请参阅 operator!=() 。
bool operator!=(const QMap<Key, T>::iterator &lhs, const QMap<Key, T>::iterator &rhs)
如果lhs 指向与rhs 迭代器不同的项目,则返回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.