IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

key_iterator Class

The QMultiHash::key_iterator class provides an STL-style const iterator for QMultiHash keys.

This class was introduced in Qt 5.6.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

key_iterator Class

  • Header: key_iterator

  • Since: Qt 5.6

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

Detailed Description

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():

 
Sélectionnez
for (QHash<int, QString>::const_iterator it = hash.cbegin(), end = hash.cend(); it != end; ++it) {
    cout << "The key: " << it.key() << Qt::endl
    cout << "The value: " << it.value() << Qt::endl;
    cout << "Also the value: " << (*it) << Qt::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.

 
Sélectionnez
// 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.

The default QMultiHash::key_iterator constructor creates an uninitialized iterator. You must initialize it using a QMultiHash function like QMultiHash::keyBegin() or 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 Implicit sharing iterator problem.

See Also

Member Function Documentation

 

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.

See Also

See also 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 QMultiHash::keyEnd() leads to undefined results.

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.

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.

See Also

See also operator!=()

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+