In questa pagina

QHash::key_iterator Class

class QHash::key_iterator

La classe ` QHash::key_iterator ` fornisce un iteratore `const` in stile STL per le chiavi ` QHash `. Maggiori informazioni...

Funzioni pubbliche

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

Descrizione dettagliata

QHash::key_iterator è sostanzialmente identica a QHash::const_iterator, con la differenza che l'operatore *() e l'operatore ->() restituiscono una chiave anziché un valore.

Nella maggior parte dei casi è consigliabile utilizzare QHash::iterator e QHash::const_iterator; è possibile accedere facilmente alla chiave chiamando 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;
}

Tuttavia, per garantire l’interoperabilità tra le chiavi di QHash e gli algoritmi in stile STL, è necessario un iteratore che dereferenzi a una chiave anziché a un valore. Con QHash::key_iterator è possibile applicare un algoritmo a un intervallo di chiavi senza dover chiamare QHash::keys(), operazione che risulta inefficiente in quanto comporta un’iterazione QHash e l’allocazione di memoria per creare un oggetto temporaneo QList.

// Inefficiente, keys() è costoso
QList<int> keys = hash.keys();
int numPrimes = std::count_if(keys.cbegin(), keys.cend(), isPrimeNumber);
qDeleteAll(hash2.keys());

// Efficiente, non richiede l'allocazione di memoria
int primeNums = std::count_if(hash.keyBegin(), hash.keyEnd(), isPrimeNumber);
qDeleteAll(hash2.keyBegin(), hash2.keyEnd());

QHash::key_iterator è una costante, non è possibile modificare la chiave.

Il costruttore predefinito di ` QHash::key_iterator ` crea un iteratore non inizializzato. È necessario inizializzarlo utilizzando una funzione ` QHash `, come ` QHash::keyBegin()` o ` QHash::keyEnd()`.

Avviso: gli iteratori su contenitori con condivisione implicita non funzionano esattamente come gli iteratori STL. È consigliabile evitare di copiare un contenitore mentre gli iteratori su di esso sono attivi. Per ulteriori informazioni, consultare Problema degli iteratori con condivisione implicita.

Vedi anche « QHash::const_iterator » e « QHash::iterator ».

Documentazione sulle funzioni membro

[noexcept default] key_iterator::key_iterator()

Crea un'istanza di ` key_iterator` con i valori predefiniti.

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

Restituisce l'const_iterator sottostante su cui si basa questo key_iterator.

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

Restituisce ` true ` se ` other ` punta a un elemento diverso da questo iteratore; altrimenti restituisce ` false`.

Vedi anche operator==().

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

Restituisce la chiave dell'elemento corrente.

[noexcept] QHash<Key, T>::key_iterator &key_iterator::operator++()

L'operatore prefissato ++ (++i) fa avanzare l'iteratore all'elemento successivo nell'hash e restituisce un iteratore puntante al nuovo elemento corrente.

La chiamata a questa funzione su QHash::keyEnd() porta a risultati indefiniti.

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

L'operatore postfisso ++ (i++) fa avanzare l'iteratore all'elemento successivo nell'hash e restituisce un iteratore all'elemento precedente.

Si tratta di una funzione sovraccaricata.

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

Restituisce un puntatore alla chiave dell'elemento corrente.

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

Restituisce ` true ` se ` other ` punta allo stesso elemento di questo iteratore; altrimenti restituisce ` false`.

Vedi anche operator!=().

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.