Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QHash< Key, T >::key_iterator Class Reference

\inmodule QtCore More...

#include <qhash.h>

+ Collaboration diagram for QHash< Key, T >::key_iterator:

Public Types

typedef const_iterator::iterator_category iterator_category
 
typedef qptrdiff difference_type
 
typedef Key value_type
 
typedef const Keypointer
 
typedef const Keyreference
 

Public Member Functions

 key_iterator () noexcept=default
 
 key_iterator (const_iterator o) noexcept
 
const Keyoperator* () const noexcept
 Returns the current item's key.
 
const Keyoperator-> () const noexcept
 Returns a pointer to the current item's key.
 
bool operator== (key_iterator o) const noexcept
 Returns true if other points to the same item as this iterator; otherwise returns false.
 
bool operator!= (key_iterator o) const noexcept
 Returns true if other points to a different item than this iterator; otherwise returns false.
 
key_iteratoroperator++ () noexcept
 The prefix ++ operator ({++i}) advances the iterator to the next item in the hash and returns an iterator to the new current item.
 
key_iterator operator++ (int) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.The postfix ++ operator ({i++}) advances the iterator to the next item in the hash and returns an iterator to the previous item.
 
const_iterator base () const noexcept
 Returns the underlying const_iterator this key_iterator is based on.
 

Detailed Description

template<typename Key, typename T>
class QHash< Key, T >::key_iterator

\inmodule QtCore

Since
5.6

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

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

For most uses QHash::iterator and QHash::const_iterator should be used, you can easily access the key by calling QHash::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 QHash's keys and STL-style algorithms we need an iterator that dereferences to a key instead of a value. With QHash::key_iterator we can apply an algorithm to a range of keys without having to call QHash::keys(), which is inefficient as it costs one QHash 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());

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

The default QHash::key_iterator constructor creates an uninitialized iterator. You must initialize it using a QHash function like QHash::keyBegin() or QHash::keyEnd().

Warning
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 \l{Implicit sharing iterator problem}.
See also
QHash::const_iterator, QHash::iterator

Definition at line 1184 of file qhash.h.

Member Typedef Documentation

◆ difference_type

template<typename Key , typename T >
QHash< Key, T >::key_iterator::difference_type

Definition at line 1190 of file qhash.h.

◆ iterator_category

template<typename Key , typename T >
QHash< Key, T >::key_iterator::iterator_category

Definition at line 1189 of file qhash.h.

◆ pointer

template<typename Key , typename T >
QHash< Key, T >::key_iterator::pointer

Definition at line 1192 of file qhash.h.

◆ reference

template<typename Key , typename T >
QHash< Key, T >::key_iterator::reference

Definition at line 1193 of file qhash.h.

◆ value_type

template<typename Key , typename T >
QHash< Key, T >::key_iterator::value_type

Definition at line 1191 of file qhash.h.

Constructor & Destructor Documentation

◆ key_iterator() [1/2]

template<typename Key , typename T >
QHash< Key, T >::key_iterator::key_iterator ( )
defaultnoexcept

Referenced by QHash< Key, T >::key_iterator::operator++().

+ Here is the caller graph for this function:

◆ key_iterator() [2/2]

template<typename Key , typename T >
QHash< Key, T >::key_iterator::key_iterator ( const_iterator o)
inlineexplicitnoexcept

Definition at line 1196 of file qhash.h.

Member Function Documentation

◆ base()

template<typename Key , typename T >
template< class Key, class T > const_iterator QHash< Key, T >::key_iterator::base ( ) const
inlinenoexcept

Returns the underlying const_iterator this key_iterator is based on.

Definition at line 1205 of file qhash.h.

References i.

◆ operator!=()

template<typename Key , typename T >
template< class Key, class T > bool QHash< Key, T >::key_iterator::operator!= ( key_iterator other) const
inlinenoexcept

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

See also
operator==()

Definition at line 1201 of file qhash.h.

References i, and o.

◆ operator*()

template<typename Key , typename T >
template< class Key, class T > const T & QHash< Key, T >::key_iterator::operator* ( ) const
inlinenoexcept

Returns the current item's key.

Definition at line 1198 of file qhash.h.

References i.

◆ operator++() [1/2]

template<typename Key , typename T >
template< class Key, class T > QHash< Key, T >::key_iterator & QHash< Key, T >::key_iterator::operator++ ( )
inlinenoexcept

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

Definition at line 1203 of file qhash.h.

References i.

◆ operator++() [2/2]

template<typename Key , typename T >
template< class Key, class T > QHash< Key, T >::key_iterator QHash< Key, T >::key_iterator::operator++ ( int )
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.The postfix ++ operator ({i++}) advances the iterator to the next item in the hash and returns an iterator to the previous item.

Definition at line 1204 of file qhash.h.

References QHash< Key, T >::key_iterator::key_iterator(), and i.

+ Here is the call graph for this function:

◆ operator->()

template<typename Key , typename T >
template< class Key, class T > const T * QHash< Key, T >::key_iterator::operator-> ( ) const
inlinenoexcept

Returns a pointer to the current item's key.

Definition at line 1199 of file qhash.h.

References i.

◆ operator==()

template<typename Key , typename T >
template< class Key, class T > bool QHash< Key, T >::key_iterator::operator== ( key_iterator other) const
inlinenoexcept

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

See also
operator!=()

Definition at line 1200 of file qhash.h.

References i, and o.


The documentation for this class was generated from the following files: