QHash::key_iterator Class
class QHash::key_iteratorLa clase QHash::key_iterator proporciona un iterador const de estilo STL para las claves de QHash. Más...
Funciones públicas
| 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 |
Descripción detallada
QHash::key_iterator es esencialmente lo mismo que QHash::const_iterator con la diferencia de que operator*() y operator->() devuelven una clave en lugar de un valor.
Para la mayoría de los usos se debe utilizar QHash::iterator y QHash::const_iterator, se puede acceder fácilmente a la clave llamando a 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; }
Sin embargo, para tener interoperabilidad entre las claves de QHash y los algoritmos de estilo STL necesitamos un iterador que haga referencia a una clave en lugar de a un valor. Con QHash::key_iterator podemos aplicar un algoritmo a un rango de claves sin tener que llamar a QHash::keys(), lo cual es ineficiente ya que cuesta una iteración QHash y la asignación de memoria para crear un QList temporal.
// Ineficiente, keys() es caroQList<int> keys = hash.keys();int numPrimes = std::count_if(keys.cbegin(), keys.cend(), isPrimeNumber);qDeleteAll(hash2.keys()); // Eficiente, no necesita asignación de memoriaint primeNums = std::count_if(hash.keyBegin(), hash.keyEnd(), isPrimeNumber);qDeleteAll(hash2.keyBegin(), hash2.keyEnd());
QHash::key_iterator es const, no es posible modificar la clave.
El constructor por defecto QHash::key_iterator crea un iterador no inicializado. Debes inicializarlo usando una función QHash como QHash::keyBegin() o QHash::keyEnd().
Advertencia: Los iteradores sobre contenedores implícitamente compartidos no funcionan exactamente como los iteradores STL. Debes evitar copiar un contenedor mientras los iteradores estén activos en ese contenedor. Para más información, lee Problema de los iteradores compartidos implícitamente.
Véase también QHash::const_iterator y QHash::iterator.
Documentación de funciones miembro
[noexcept] QHash<Key, T>::const_iterator key_iterator::base() const
Devuelve el const_iterator subyacente en el que se basa este key_iterator.
[noexcept] bool key_iterator::operator!=(QHash<Key, T>::key_iterator other) const
Devuelve true si other apunta a un elemento distinto de este iterador; en caso contrario devuelve false.
Véase también operator==().
[noexcept] const Key &key_iterator::operator*() const
Devuelve la clave del elemento actual.
[noexcept] QHash<Key, T>::key_iterator &key_iterator::operator++()
El operador prefijo ++ (++i) avanza el iterador al siguiente elemento del hash y devuelve un iterador al nuevo elemento actual.
Llamar a esta función en QHash::keyEnd() conduce a resultados indefinidos.
[noexcept] QHash<Key, T>::key_iterator key_iterator::operator++(int)
El operador postfijo ++ (i++) avanza el iterador al siguiente elemento del hash y devuelve un iterador al elemento anterior.
Se trata de una función sobrecargada.
[noexcept] const Key *key_iterator::operator->() const
Devuelve un puntero a la clave del elemento actual.
[noexcept] bool key_iterator::operator==(QHash<Key, T>::key_iterator other) const
Devuelve true si other apunta al mismo elemento que este iterador; en caso contrario devuelve false.
Véase también 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.