iterator Class

class QHash::iterator

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

공용 함수

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

상세 설명

QHash<키, T>::이터레이터를 사용하면 QHash 를 반복하고 특정 키와 연관된 값(키는 아님)을 수정할 수 있습니다. QHash 을 반복하려면 QHash::const_iterator 을 사용해야 합니다. 일반적으로 이터레이터를 통해 QHash 을 변경해야 하는 경우가 아니라면, 상수가 아닌 QHash 에도 QHash::const_iterator 을 사용하는 것이 좋습니다. Const 이터레이터는 약간 더 빠르며 코드 가독성을 향상시킬 수 있습니다.

기본 QHash::iterator 생성자는 초기화되지 않은 이터레이터를 생성합니다. QHash::begin (), QHash::end() 또는 QHash::find()와 같은 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(key()) << ": " << i.value() << endl;

키별로 항목을 정렬하는 QMap 와 달리 QHash 은 항목을 임의의 순서로 저장합니다.

다음은 QHash 에 저장된 모든 값을 2씩 증가시키는 예제입니다:

for (auto i = hash.begin(), end = hash.end(); i != end; ++i)
    i.value() += 2;

QHash 에서 요소를 제거하려면 erase_if(QHash<키, T> &map, 술어 pred)를 사용할 수 있습니다:

erase_if(hash, [](const QHash<QString, int>::iterator it) { return it.value() > 10; });

동일한 해시에서 여러 개의 반복기를 사용할 수 있습니다. 그러나 QHash 에서 직접 수정(항목 삽입 및 제거)을 수행하면 반복기가 유효하지 않게 될 수 있다는 점에 유의하세요.

해시에 항목을 삽입하거나 QHash::reserve() 또는 QHash::squeeze()와 같은 메서드를 호출하면 해시를 가리키는 모든 이터레이터가 무효화될 수 있습니다. 이터레이터는 QHash 가 내부 해시 테이블을 늘리거나 줄일 필요가 없는 한 유효하게 유지됩니다. 리해싱 작업이 발생한 후에 반복자를 사용하면 정의되지 않은 동작이 발생할 수 있습니다.

장기간에 걸쳐 반복자를 유지해야 하는 경우 QHash 보다는 QMap 을 사용하는 것이 좋습니다.

경고: 암시적으로 공유되는 컨테이너의 이터레이터는 STL 이터레이터와 똑같이 작동하지 않습니다. 해당 컨테이너에서 이터레이터가 활성화되어 있는 동안에는 컨테이너를 복사하지 않아야 합니다. 자세한 내용은 암시적 공유 이터레이터 문제를 참조하세요.

QHash::const_iterator, QHash::key_iterator, QHash::key_value_iterator도 참조하세요 .

멤버 함수 문서

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

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

other 이 이터레이터와 동일한 항목을 가리키면 true 을 반환하고, 그렇지 않으면 false 을 반환합니다.

operator!=()도 참조하세요 .

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

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

other 이 이터레이터와 다른 항목을 가리키면 true 을 반환하고, 그렇지 않으면 false 을 반환합니다.

operator==()도 참조하세요 .

[constexpr noexcept] iterator::iterator()

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

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

QHash::begin() 및 QHash::end()도 참조하세요 .

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

현재 항목의 키를 상수 참조로 반환합니다.

반복기를 통해 항목의 키를 직접 변경하는 방법은 없지만 QHash::erase() 뒤에 QHash::insert()를 호출하여 변경할 수 있습니다.

value()도 참조하세요 .

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

현재 항목의 값에 대한 수정 가능한 참조를 반환합니다.

예를 들어 할당의 왼쪽에 있는 value()를 사용하여 항목의 값을 변경할 수 있습니다:

if (i.key() == "Hello")
    i.value() = "Bonjour";

key() 및 operator*()도 참조하세요 .

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

현재 항목의 값에 대한 수정 가능한 참조를 반환합니다.

value()와 동일합니다.

key()도 참조하세요 .

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

접두사 ++ 연산자(++i)는 반복자를 해시의 다음 항목으로 전진시키고 반복자를 새 현재 항목으로 반환합니다.

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

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

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

후위 ++ 연산자(i++)는 반복자를 해시의 다음 항목으로 전진시키고 반복자를 이전 현재 항목으로 반환합니다.

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

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

value()도 참조하세요 .

© 2025 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.