key_iterator Class

(QMap::key_iterator)

The QMap::key_iterator class provides an STL-style const iterator for QMap and QMultiMap keys. 更多...

头文件: #include <key_iterator>
qmake: QT += core
开始支持版本: Qt 5.6

公有函数

key_iterator() = default
key_iterator(const_iterator o)
const_iterator base() const
bool operator!=(key_iterator other) const
const Key &operator*() const
key_iterator &operator++()
key_iterator operator++(int)
key_iterator &operator--()
key_iterator operator--(int)
const Key *operator->() const
bool operator==(key_iterator other) const

详细描述

QMap::key_iteratorQMapQMultiMap 的键提供 STL 风格的常量类型迭代器.

除了 operator*() 和 operator->() 返回键而不是值以外, QMap::key_iterator 基本和 QMap::const_iterator 相同.

多数情况下应该使用 QMap::iteratorQMap::const_iterator, 通过调用 QMap::iterator::key() 可以很容易地取得键:


  for (QMap<int, QString>::const_iterator it = map.cbegin(), end = map.cend(); it != end; ++it) {
      cout << "The key: " << it.key() << endl
      cout << "The value: " << it.value() << endl;
      cout << "Also the value: " << (*it) << endl;
  }

然而, 如果想对 QMap 的键应用 STL 算法, 就需要一个能解引用出键而不是值的迭代器. 通过 QMap::key_iterator 就能直接对一组键应用 STL 算法而不必调用 QMap::keys(), 这个函数比较低效, 因为它首先要遍历一遍 QMap, 然后分配内存创建一个临时的 QList.


  // Inefficient, keys() is expensive
  QList<int> keys = map.keys();
  int numPrimes = std::count_if(map.cbegin(), map.cend(), isPrimeNumber);
  qDeleteAll(map2.keys());

  // Efficient, no memory allocation needed
  int numPrimes = std::count_if(map.keyBegin(), map.keyEnd(), isPrimeNumber);
  qDeleteAll(map2.keyBegin(), map2.keyEnd());

QMap::key_iterator 是 const, 键是不可能被修改的.

QMap::key_iterator 的默认构造函数创建一个未初始化的迭代器. 你必须使用 QMap::keyBegin() 或 QMap::keyEnd() 等 QMap 函数来初始化它.

警告: 隐式共享容器的迭代器的工作方式和 STL 迭代器不完全相同. 当容器的迭代器还处于活动状态时, 应该避免拷贝容器. 详见 隐式共享迭代器问题.

参见 QMap::const_iteratorQMap::iterator.

成员函数

[default] key_iterator::key_iterator()

Default constructs an instance of key_iterator.

key_iterator::key_iterator(const_iterator o)

Default constructs an instance of key_iterator.

const_iterator key_iterator::base() const

Returns the underlying const_iterator this key_iterator is based on.

bool key_iterator::operator!=(key_iterator other) const

Returns true if other points to a different item than this iterator; otherwise returns false.

参见 operator==().

const Key &key_iterator::operator*() const

Returns the current item's key.

key_iterator &key_iterator::operator++()

The prefix ++ operator (++i) advances the iterator to the next item in the hash and returns an iterator to the new current item.

Calling this function on QMap::keyEnd() leads to undefined results.

参见 operator--().

key_iterator key_iterator::operator++(int)

This is an overloaded function.

The postfix ++ operator (i++) advances the iterator to the next item in the hash and returns an iterator to the previous item.

key_iterator &key_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::keyBegin() leads to undefined results.

参见 operator++().

key_iterator key_iterator::operator--(int)

This is an overloaded function.

The postfix -- operator (i--) makes the preceding item current and returns an iterator pointing to the previous item.

const Key *key_iterator::operator->() const

Returns a pointer to the current item's key.

bool key_iterator::operator==(key_iterator other) const

Returns true if other points to the same item as this iterator; otherwise returns false.

参见 operator!=().