iterator Class
(QMap::iterator)The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap. 更多...
头文件: | #include <iterator> |
qmake: | QT += core |
公有类型
typedef | iterator_category |
公有函数
iterator() | |
const Key & | key() const |
T & | value() const |
bool | operator!=(const iterator &other) const |
bool | operator!=(const const_iterator &other) const |
T & | operator*() const |
iterator | operator+(int j) const |
iterator & | operator++() |
iterator | operator++(int) |
iterator & | operator+=(int j) |
iterator | operator-(int j) const |
iterator & | operator--() |
iterator | operator--(int) |
iterator & | operator-=(int j) |
T * | operator->() const |
bool | operator==(const iterator &other) const |
bool | operator==(const const_iterator &other) const |
详细描述
The QMap::iterator 为 QMap 和 QMultiMap提供 STL 风格的非常量迭代器.
QMap 同时提供 STL 风格迭代器 和 Java 风格迭代器. STL 风格迭代器更底层, 使用更笨拙; 同时也稍快一些. 对于已经了解 STL 的开发者来说更加熟悉.
QMap<Key, T>::iterator 用来遍历 QMap (或 QMultiMap), 并修改与特定键相关联的值(不能修改键). 如果你想遍历常量 QMap, 你应该使用 QMap::const_iterator. 对于非常量 QMap, 使用 QMap::const_iterator 通常是好的编程实践, 除非需要在遍历时改变 QMap. 常量类型的迭代器稍快一些并可以提高代码可读性.
QMap::iterator 的默认构造函数创建一个未初始化的迭代器. 你必须在开始遍历前使用 QMap::begin(), QMap::end(), 或 QMap::find() 等QMap函数初始化它. 下面是一个典型的循环, 该循环打印出 map 中的所有键值对:
QMap<QString, int> map; map.insert("January", 1); map.insert("February", 2); ... map.insert("December", 12); QMap<QString, int>::iterator i; for (i = map.begin(); i != map.end(); ++i) cout << i.key() << ": " << i.value() << endl;
与无序存储元素的 QHash 不同, QMap 通过键的大小有序存储元素. 相同键的元素, 将按照从最新到最早插入的顺序连续出现.
让我们通过几个例子来了解哪些情况下可以用 QMap::iterator, 而不能用 QMap::const_iterator. 下面的例子将存储在 QMap 中的每个值增加2:
QMap<QString, int>::iterator i; for (i = map.begin(); i != map.end(); ++i) i.value() += 2;
下面的例子移除所有键字符串的首字符是下划线的元素:
QMap<QString, int>::iterator i = map.begin(); while (i != map.end()) { if (i.key().startsWith('_')) i = map.erase(i); else ++i; }
调用 QMap::erase() 从 map 中移除迭代器所指元素, 返回指向下一个元素的迭代器. 下面是另一个在遍历时移除元素的方法:
QMap<QString, int>::iterator i = map.begin(); while (i != map.end()) { QMap<QString, int>::iterator prev = i; ++i; if (prev.key().startsWith('_')) map.erase(prev); }
有人会写出像下面这样的代码:
// WRONG while (i != map.end()) { if (i.key().startsWith('_')) map.erase(i); ++i; }
然而, 这会导致程序在 ++i
处崩溃, 因为调用完 erase()后, i
成为悬空迭代器.
同一 map 可以使用多个迭代器. 如果向 map 添加元素, 已有的迭代器仍然有效. 如果从 map 移除元素, 指向被移除元素的迭代器会变成悬空迭代器.
警告: 隐式共享容器迭代器的工作方式和 STL 迭代器不完全相同. 当容器的迭代器还处于活动状态时, 应该避免拷贝容器. 详见 隐式共享迭代器问题.
参见 QMap::const_iterator, QMap::key_iterator, QMutableMapIterator.
成员类型
typedef iterator::iterator_category
A synonym for std::bidirectional_iterator_tag indicating this iterator is a bidirectional iterator.
成员函数
iterator::iterator()
Constructs an uninitialized iterator.
Functions like key(), value(), and operator++() must not be called on an uninitialized iterator. Use operator=() to assign a value to it before using it.
参见 QMap::begin() and QMap::end().
const Key &iterator::key() const
Returns the current item's key as a const reference.
There is no direct way of changing an item's key through an iterator, although it can be done by calling QMap::erase() followed by QMap::insert() or QMap::insertMulti().
参见 value().
T &iterator::value() const
Returns a modifiable reference to the current item's value.
You can change the value of an item by using value() on the left side of an assignment, for example:
if (i.key() == "Hello") i.value() = "Bonjour";
bool iterator::operator!=(const iterator &other) const
Returns true
if other points to a different item than this iterator; otherwise returns false
.
参见 operator==().
bool iterator::operator!=(const const_iterator &other) const
Returns true
if other points to a different item than this iterator; otherwise returns false
.
参见 operator==().
T &iterator::operator*() const
Returns a modifiable reference to the current item's value.
Same as value().
参见 key().
iterator iterator::operator+(int j) const
Returns an iterator to the item at j positions forward from this iterator. (If j is negative, the iterator goes backward.)
This operation can be slow for large j values.
参见 operator-().
iterator &iterator::operator++()
The prefix ++ operator (++i
) advances the iterator to the next item in the map and returns an iterator to the new current item.
Calling this function on QMap::end() leads to undefined results.
参见 operator--().
iterator iterator::operator++(int)
This is an overloaded function.
The postfix ++ operator (i++
) advances the iterator to the next item in the map and returns an iterator to the previously current item.
iterator &iterator::operator+=(int j)
Advances the iterator by j items. (If j is negative, the iterator goes backward.)
参见 operator-=() and operator+().
iterator iterator::operator-(int j) const
Returns an iterator to the item at j positions backward from this iterator. (If j is negative, the iterator goes forward.)
This operation can be slow for large j values.
参见 operator+().
iterator &iterator::operator--()
The prefix -- operator (--i
) makes the preceding item current and returns an iterator pointing to the new current item.
Calling this function on QMap::begin() leads to undefined results.
参见 operator++().
iterator iterator::operator--(int)
This is an overloaded function.
The postfix -- operator (i--
) makes the preceding item current and returns an iterator pointing to the previously current item.
iterator &iterator::operator-=(int j)
Makes the iterator go back by j items. (If j is negative, the iterator goes forward.)
参见 operator+=() and operator-().
T *iterator::operator->() const
Returns a pointer to the current item's value.
参见 value().
bool iterator::operator==(const iterator &other) const
Returns true
if other points to the same item as this iterator; otherwise returns false
.
参见 operator!=().
bool iterator::operator==(const const_iterator &other) const
Returns true
if other points to the same item as this iterator; otherwise returns false
.
参见 operator!=().