QHash::const_iterator Class
class QHash::const_iteratorLa clase QHash::const_iterator proporciona un iterador const de estilo STL para QHash. Más...
Funciones públicas
| const_iterator() | |
| const_iterator(const QHash<Key, T>::iterator &other) | |
| const Key & | key() const |
| const T & | value() const |
| bool | operator!=(const QHash<Key, T>::const_iterator &other) const |
| const T & | operator*() const |
| QHash<Key, T>::const_iterator & | operator++() |
| QHash<Key, T>::const_iterator | operator++(int) |
| const T * | operator->() const |
| bool | operator==(const QHash<Key, T>::const_iterator &other) const |
Descripción detallada
QHash<Key, T>::const_iterator le permite iterar sobre un QHash. Si desea modificar el QHash mientras itera sobre él, debe utilizar QHash::iterator en su lugar. Generalmente es una buena práctica usar QHash::const_iterator sobre un QHash no-const también, a menos que necesites cambiar el QHash a través del iterador. Los iteradores const son ligeramente más rápidos y pueden mejorar la legibilidad del código.
El constructor por defecto QHash::const_iterator crea un iterador no inicializado. Debes inicializarlo usando una función QHash como QHash::cbegin(), QHash::cend(), o QHash::constFind() antes de que puedas empezar a iterar. Este es un bucle típico que imprime todos los pares (clave, valor) almacenados en un hash:
QHash<QString, int> hash; hash.insert("January", 1); hash.insert("February", 2); //... hash.insert("December", 12); for (auto i = hash.cbegin(), end = hash.cend(); i != end; ++i) cout << qPrintable(i.key()) << ": " << i.value() << endl;
A diferencia de QMap, que ordena sus elementos por clave, QHash almacena sus elementos en un orden arbitrario. La única garantía es que los elementos que comparten la misma clave (porque se insertaron utilizando un QMultiHash) aparecerán consecutivamente, desde el valor insertado más recientemente hasta el menos reciente.
Se pueden utilizar varios iteradores en el mismo hash. Sin embargo, tenga en cuenta que cualquier modificación realizada directamente en QHash (inserción y eliminación de elementos) puede hacer que los iteradores pierdan su validez.
Insertar elementos en el hash o llamar a métodos como QHash::reserve() o QHash::squeeze() puede invalidar todos los iteradores que apunten al hash. Se garantiza que los iteradores seguirán siendo válidos mientras QHash no tenga que aumentar/reducir su tabla hash interna. El uso de cualquier iterador después de que se haya producido una operación de reflash dará lugar a un comportamiento indefinido.
Sin embargo, puedes utilizar iteradores de forma segura para eliminar entradas del hash utilizando el método QHash::erase(). Esta función puede ser llamada de forma segura mientras se itera, y no afectará al orden de los elementos en el hash.
Advertencia: Los iteradores en contenedores implícitamente compartidos no funcionan exactamente como los iteradores STL. Deberías 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::iterator, QHash::key_iterator, y QHash::const_key_value_iterator.
Documentación de funciones miembro
[constexpr noexcept] const_iterator::const_iterator()
Construye un iterador no inicializado.
Funciones como key(), value(), y operator++() no deben ser llamadas en un iterador no inicializado. Utilice operator=() para asignarle un valor antes de utilizarlo.
Véase también QHash::constBegin() y QHash::constEnd().
[noexcept] const_iterator::const_iterator(const QHash<Key, T>::iterator &other)
Construye una copia de other.
[noexcept] const Key &const_iterator::key() const
Devuelve la clave del elemento actual.
Véase también value().
[noexcept] const T &const_iterator::value() const
Devuelve el valor del elemento actual.
Véase también key() y operator*().
[noexcept] bool const_iterator::operator!=(const QHash<Key, T>::const_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 T &const_iterator::operator*() const
Devuelve el valor del elemento actual.
Igual que value().
Véase también key().
[noexcept] QHash<Key, T>::const_iterator &const_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::end() conduce a resultados indefinidos.
[noexcept] QHash<Key, T>::const_iterator const_iterator::operator++(int)
El operador postfijo ++ (i++) avanza el iterador al siguiente elemento del hash y devuelve un iterador al elemento previamente actual.
Se trata de una función sobrecargada.
[noexcept] const T *const_iterator::operator->() const
Devuelve un puntero al valor del elemento actual.
Véase también value().
[noexcept] bool const_iterator::operator==(const QHash<Key, T>::const_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.