QHashIterator Class

QHashIteratorQHashQMultiHash 提供 Java 风格的常量迭代器. 更多...

头文件: #include <QHashIterator>
qmake: QT += core

公有函数

QHashIterator(const QHash<Key, T> &hash)
bool findNext(const T &value)
bool findPrevious(const T &value)
bool hasNext() const
bool hasPrevious() const
const Key &key() const
Item next()
Item peekNext() const
Item peekPrevious() const
Item previous()
void toBack()
void toFront()
const T &value() const
QHashIterator &operator=(const QHash<Key, T> &hash)

详细描述

The QHashIteratorQHash and QMultiHash 提供 Java 风格的常量迭代器.

QHash 同时提供 Java 风格迭代器STL 风格迭代器. Java 风格迭代器比 STL 风格迭代器更高级, 更容易使用; 同时也略微低效.

QHashIterator<Key, T> 可以遍历 QHash (或 QMultiHash) 所有元素. 如果你想在遍历时修改哈希表, 使用 QMutableHashIterator.

QHashIterator 构造函数接受 QHash 作为参数. 构造后, 迭代器位于哈希表的最开始位置 (第一个元素之前). 下面的例子演示如何顺序遍历所有元素:


  QHash<int, QWidget *> hash;
  ...
  QHashIterator<int, QWidget *> i(hash);
  while (i.hasNext()) {
      i.next();
      qDebug() << i.key() << ": " << i.value();
  }

next() 函数返回哈希表中的下一个元素并将迭代器前移. key() 和 value() 函数返回跳过的最后一个元素的键和值.

与 STL 风格迭代器不同, Java 风格迭代器指向元素 之间, 而不是直接 指向 元素. 第一次调用 next() 前移迭代器到第一个和第二个元素之间的位置, 并返回第一个元素; 第二次调用 next() 前移迭代器到第二个和第三个元素之间的位置; 以此类推.

下列示例是如何遍历所有元素:


  QHashIterator<int, QWidget *> i(hash);
  i.toBack();
  while (i.hasPrevious()) {
      i.previous();
      qDebug() << i.key() << ": " << i.value();
  }

如果你想查找特定值的所有实例, 循环使用 findNext() 或 findPrevious(). 例如:


  QHashIterator<int, QWidget *> i(hash);
  while (i.findNext(widget)) {
      qDebug() << "Found widget " << widget << " under key "
               << i.key();
  }

同一哈希表可以使用多个迭代器. 如果在 QHashIterator 处于活动状态时修改哈希表, QHashIterator 将继续在原哈希表上遍历, 而忽略修改后的副本.

参见 QMutableHashIteratorQHash::const_iterator.

成员函数

QHashIterator::QHashIterator(const QHash<Key, T> &hash)

Constructs an iterator for traversing hash. The iterator is set to be at the front of the hash (before the first item).

参见 operator=().

bool QHashIterator::findNext(const T &value)

Searches for value starting from the current iterator position forward. Returns true if a (key, value) pair with value value is found; otherwise returns false.

After the call, if value was found, the iterator is positioned just after the matching item; otherwise, the iterator is positioned at the back of the container.

参见 findPrevious().

bool QHashIterator::findPrevious(const T &value)

Searches for value starting from the current iterator position backward. Returns true if a (key, value) pair with value value is found; otherwise returns false.

After the call, if value was found, the iterator is positioned just before the matching item; otherwise, the iterator is positioned at the front of the container.

参见 findNext().

bool QHashIterator::hasNext() const

Returns true if there is at least one item ahead of the iterator, i.e. the iterator is not at the back of the container; otherwise returns false.

参见 hasPrevious() and next().

bool QHashIterator::hasPrevious() const

Returns true if there is at least one item behind the iterator, i.e. the iterator is not at the front of the container; otherwise returns false.

参见 hasNext() and previous().

const Key &QHashIterator::key() const

Returns the key of the last item that was jumped over using one of the traversal functions (next(), previous(), findNext(), findPrevious()).

After a call to next() or findNext(), key() is equivalent to peekPrevious().key(). After a call to previous() or findPrevious(), key() is equivalent to peekNext().key().

参见 value().

Item QHashIterator::next()

Returns the next item and advances the iterator by one position.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the back of the container leads to undefined results.

参见 hasNext(), peekNext(), and previous().

Item QHashIterator::peekNext() const

Returns the next item without moving the iterator.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the back of the container leads to undefined results.

参见 hasNext(), next(), and peekPrevious().

Item QHashIterator::peekPrevious() const

Returns the previous item without moving the iterator.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the front of the container leads to undefined results.

参见 hasPrevious(), previous(), and peekNext().

Item QHashIterator::previous()

Returns the previous item and moves the iterator back by one position.

Call key() on the return value to obtain the item's key, and value() to obtain the value.

Calling this function on an iterator located at the front of the container leads to undefined results.

参见 hasPrevious(), peekPrevious(), and next().

void QHashIterator::toBack()

Moves the iterator to the back of the container (after the last item).

参见 toFront() and previous().

void QHashIterator::toFront()

Moves the iterator to the front of the container (before the first item).

参见 toBack() and next().

const T &QHashIterator::value() const

Returns the value of the last item that was jumped over using one of the traversal functions (next(), previous(), findNext(), findPrevious()).

After a call to next() or findNext(), value() is equivalent to peekPrevious().value(). After a call to previous() or findPrevious(), value() is equivalent to peekNext().value().

参见 key().

QHashIterator &QHashIterator::operator=(const QHash<Key, T> &hash)

Makes the iterator operate on hash. The iterator is set to be at the front of the hash (before the first item).

参见 toFront() and toBack().