이 페이지에서

QHash::const_iterator Class

class QHash::const_iterator

QHash::const_iterator 클래스는 ` QHash`에 대한 STL 스타일의 const 이터레이터를 제공합니다. 더 보기...

공개 함수

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

상세 설명

QHash<Key, T>::const_iterator를 사용하면 QHash 을 순회할 수 있습니다. 순회하는 동안 QHash 을 수정하려면 대신 QHash::iterator 를 사용해야 합니다. 이터레이터를 통해 ` QHash `을 변경해야 하는 경우가 아니라면, `non-const` ` QHash `에 대해서도 ` QHash::const_iterator `을 사용하는 것이 일반적으로 권장되는 관행입니다. `const` 이터레이터는 속도가 약간 더 빠르며 코드의 가독성을 높일 수 있습니다.

QHash::const_iterator 의 기본 생성자는 초기화되지 않은 이터레이터를 생성합니다. 반복 처리를 시작하기 전에 QHash::cbegin(), QHash::cend(), 또는 QHash::constFind()와 같은 QHash 함수를 사용하여 이터레이터를 초기화해야 합니다. 다음은 해시에 저장된 모든 (키, 값) 쌍을 출력하는 일반적인 루프 예시입니다:

    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;

항목을 키 순서대로 정렬하는 QMap 와는 달리, QHash 는 항목을 임의의 순서로 저장합니다. 유일하게 보장되는 점은, 동일한 키를 공유하는 항목( QMultiHash 를 사용하여 삽입된 경우)이 가장 최근에 삽입된 값부터 가장 오래전에 삽입된 값 순으로 연속적으로 나타난다는 것입니다.

동일한 해시에 여러 개의 이터레이터를 사용할 수 있습니다. 그러나 QHash 에 직접 수행하는 모든 수정 작업(항목 삽입 및 제거)은 이터레이터를 무효화할 수 있으므로 주의해야 합니다.

해시에 항목을 삽입하거나 ` QHash::reserve()` 또는 ` QHash::squeeze()`와 같은 메서드를 호출하면 해시를 가리키는 모든 이터레이터가 무효화될 수 있습니다. 이터레이터는 QHash 가 내부 해시 테이블을 확장하거나 축소할 필요가 없는 경우에만 유효함이 보장됩니다. 재해시(rehashing) 작업이 발생한 후 이터레이터를 사용하면 정의되지 않은 동작이 발생할 수 있습니다.

그러나 ` QHash::erase()` 메서드를 사용하여 해시에서 항목을 제거할 때는 이터레이터를 안전하게 사용할 수 있습니다. 이 함수는 반복 처리 중에도 안전하게 호출할 수 있으며, 해시 내 항목의 순서에 영향을 미치지 않습니다.

경고: 암시적으로 공유되는 컨테이너의이터레이터는 STL 이터레이터와 정확히 동일하게 작동하지 않습니다. 해당 컨테이너에서 이터레이터가 활성화된 상태에서는 컨테이너를 복사하지 않도록 해야 합니다. 자세한 내용은 ‘암시적 공유 이터레이터 문제’를 참조하십시오.

참조: QHash::iterator, QHash::key_iterator 및 QHash::const_key_value_iterator.

멤버 함수 문서

[constexpr noexcept default] const_iterator::const_iterator()

초기화되지 않은 이터레이터를 생성합니다.

key(), value() 및 operator++()와 같은 함수는 초기화되지 않은 이터레이터에 대해 호출해서는 안 됩니다. 이터레이터를 사용하기 전에 operator=()를 사용하여 값을 할당하십시오.

QHash::constBegin() 및 QHash::constEnd()도 참조하십시오 .

[noexcept] const_iterator::const_iterator(const QHash<Key, T>::iterator &other)

other 의 복사본을 생성합니다.

[noexcept] const Key &const_iterator::key() const

현재 항목의 키를 반환합니다.

value()도 참조하십시오 .

[noexcept] const T &const_iterator::value() const

현재 항목의 값을 반환합니다.

key() 및 operator*()도 참조하십시오 .

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

other 가 이 반복자가 가리키는 항목과 다른 항목을 가리키는 경우 true 를 반환하고, 그렇지 않은 경우 false 를 반환합니다.

operator==()도 참조하십시오 .

[noexcept] const T &const_iterator::operator*() const

현재 항목의 값을 반환합니다.

value()와 동일합니다.

key()도 참조하십시오 .

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

접두사 ++ 연산자(++i)는 이터레이터를 해시 내의 다음 항목으로 이동시키고, 새로운 현재 항목에 대한 이터레이터를 반환합니다.

QHash::end()에서 이 함수를 호출하면 정의되지 않은 결과가 발생합니다.

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

후치 연산자 ++(i++)는 이터레이터를 해시의 다음 항목으로 이동시키고, 이전에 현재 항목이었던 항목에 대한 이터레이터를 반환합니다.

이는 오버로드된 함수입니다.

[noexcept] const T *const_iterator::operator->() const

현재 항목의 값에 대한 포인터를 반환합니다.

value()도 참조하십시오 .

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

other 가 이 반복자와 동일한 항목을 가리키는 경우 true 를 반환하고, 그렇지 않은 경우 false 를 반환합니다.

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.