QMultiHash::key_iterator Class

class QMultiHash ::key_iterator

The QMultiHash::key_iterator 類提供 STL 樣式 const 迭代器為 QMultiHash keys. 更多...

公共函數

QMultiHash<Key, T>::const_iterator base () const
bool operator!= (QMultiHash<Key, T>::key_iterator other ) const
const Key & operator* () const
QMultiHash<Key, T>::key_iterator & operator++ ()
QMultiHash<Key, T>::key_iterator operator++ (int)
const Key * operator-> () const
bool operator== (QMultiHash<Key, T>::key_iterator other ) const

詳細描述

QMultiHash::key_iterator is essentially the same as QMultiHash::const_iterator with the difference that operator*() and operator->() return a key instead of a value.

For most uses QMultiHash::iterator and QMultiHash::const_iterator should be used, you can easily access the key by calling QMultiHash::iterator::key ():

for (auto it = hash.cbegin(), end = hash.cend(); it != end; ++it) {
    cout << "The key: " << it.key() << endl;
    cout << "The value: " << qPrintable(it.value()) << endl;
    cout << "Also the value: " << qPrintable(*it) << endl;
}
					

However, to have interoperability between QMultiHash 's keys and STL-style algorithms we need an iterator that dereferences to a key instead of a value. With QMultiHash::key_iterator we can apply an algorithm to a range of keys without having to call QMultiHash::keys (), which is inefficient as it costs one QMultiHash iteration and memory allocation to create a temporary QList .

// Inefficient, keys() is expensive
QList<int> keys = hash.keys();
int numPrimes = std::count_if(keys.cbegin(), keys.cend(), isPrimeNumber);
qDeleteAll(hash2.keys());
// Efficient, no memory allocation needed
int numPrimes = std::count_if(hash.keyBegin(), hash.keyEnd(), isPrimeNumber);
qDeleteAll(hash2.keyBegin(), hash2.keyEnd());
					

QMultiHash::key_iterator is const, it's not possible to modify the key.

默認 QMultiHash::key_iterator constructor creates an uninitialized iterator. You must initialize it using a QMultiHash function like QMultiHash::keyBegin () 或 QMultiHash::keyEnd ().

警告: Iterators on implicitly shared containers do not work exactly like STL-iterators. You should avoid copying a container while iterators are active on that container. For more information, read 隱式共享迭代器問題 .

另請參閱 QMultiHash::const_iterator and QMultiHash::iterator .

成員函數文檔編製

[noexcept] QMultiHash < Key , T > ::const_iterator key_iterator:: base () const

返迴的底層 const_iterator this key_iterator is based on.

[noexcept] bool key_iterator:: operator!= ( QMultiHash < Key , T > ::key_iterator other ) const

返迴 true if other 指嚮與此迭代器不同的項;否則返迴 false .

另請參閱 operator== ().

[noexcept] const Key &key_iterator:: operator* () const

Returns the current item's key.

[noexcept] QMultiHash < Key , T > ::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 QMultiHash::keyEnd () leads to undefined results.

[noexcept] QMultiHash < Key , T > ::key_iterator key_iterator:: operator++ ( int )

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

這是重載函數。

[noexcept] const Key *key_iterator:: operator-> () const

Returns a pointer to the current item's key.

[noexcept] bool key_iterator:: operator== ( QMultiHash < Key , T > ::key_iterator other ) const

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

另請參閱 operator!= ().