이 페이지에서

QLinkedList::iterator Class

class QLinkedList::iterator

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

공개 함수

iterator()
bool operator!=(const QLinkedList<T>::const_iterator &other) const
bool operator!=(const QLinkedList<T>::iterator &other) const
T &operator*() const
QLinkedList<T>::iterator operator+(int j) const
QLinkedList<T>::iterator &operator++()
QLinkedList<T>::iterator operator++(int)
QLinkedList<T>::iterator &operator+=(int j)
QLinkedList<T>::iterator operator-(int j) const
QLinkedList<T>::iterator &operator--()
QLinkedList<T>::iterator operator--(int)
QLinkedList<T>::iterator &operator-=(int j)
T *operator->() const
bool operator==(const QLinkedList<T>::const_iterator &other) const
bool operator==(const QLinkedList<T>::iterator &other) const

상세 설명

QLinkedList 는 STL 스타일 이터레이터와 Java 스타일 이터레이터를 모두 제공합니다. STL 스타일 이터레이터는 더 저수준이며 사용하기가 더 번거롭지만, 반면에 속도가 약간 더 빠르며 이미 STL을 알고 있는 개발자에게는 익숙하다는 장점이 있습니다.

QLinkedList<T>::iterator를 사용하면 QLinkedList<T>를 순회하고 이터레이터와 연관된 리스트 항목을 수정할 수 있습니다. const QLinkedList 를 순회하려면 대신 QLinkedList::const_iterator 를 사용하십시오. 이터레이터를 통해 QLinkedList 를 변경해야 하는 경우가 아니라면, 일반적으로 non-const QLinkedList 에 대해서도 QLinkedList::const_iterator 를 사용하는 것이 좋은 관행입니다. const 이터레이터는 속도가 약간 더 빠르며 코드의 가독성을 높일 수 있습니다.

QLinkedList::iterator 의 기본 생성자는 초기화되지 않은 이터레이터를 생성합니다. 반복 처리를 시작하기 전에 QLinkedList::begin(), QLinkedList::end() 또는 QLinkedList::insert()과 같은 함수를 사용하여 이터레이터를 초기화해야 합니다. 다음은 리스트에 저장된 모든 항목을 출력하는 일반적인 루프 예시입니다:

QLinkedList<QString> list;
list.append("January");
list.append("February");
...
list.append("December");

QLinkedList<QString>::iterator i;
for (i = list.begin(); i != list.end(); ++i)
    cout << *i << Qt::endl;

STL 스타일의 이터레이터는 generic algorithms 의 인자로 사용할 수 있습니다. 예를 들어, 리스트에서 특정 항목을 찾는 방법은 다음과 같습니다:

QLinkedList<QString> list;
...
QLinkedList<QString>::iterator it = std::find(list.begin(),
                                              list.end(), "Joel");
if (it != list.end())
    cout << "Found Joel" << Qt::endl;

QLinkedList::const_iterator 으로는 할 수 없지만 QLinkedList::iterator 로는 할 수 있는 몇 가지 예제를 살펴보겠습니다. 다음은 QLinkedList<int>에 저장된 모든 값을 2씩 증가시키는 예제입니다:

QLinkedList<int>::iterator i;
for (i = list.begin(); i != list.end(); ++i)
    *i += 2;

다음은 QLinkedList<QString>에서 밑줄(_)로 시작하는 모든 항목을 제거하는 예제입니다:

QLinkedList<QString> list;
...
QLinkedList<QString>::iterator i = list.begin();
while (i != list.end()) {
    if ((*i).startsWith('_'))
        i = list.erase(i);
    else
        ++i;
}

QLinkedList::erase()를 호출하면 이터레이터가 가리키는 항목을 목록에서 제거하고, 다음 항목을 가리키는 이터레이터를 반환합니다. 반복 처리를 하면서 항목을 제거하는 또 다른 방법은 다음과 같습니다:

QLinkedList<QString>::iterator i = list.begin();
while (i != list.end()) {
    QLinkedList<QString>::iterator previous = i;
    ++i;
    if ((*previous).startsWith('_'))
        list.erase(previous);
}

다음과 같은 코드를 작성하고 싶은 유혹이 들 수 있습니다:

// WRONG
while (i != list.end()) {
    if ((*i).startsWith('_'))
        list.erase(i);
    ++i;
}

그러나 erase()를 호출한 후 i 는 더 이상 유효하지 않은 이터레이터(dangling iterator)가 되므로, ++i 에서 잠재적으로 프로그램이 중단될 수 있습니다.

동일한 리스트에서 여러 개의 이터레이터를 사용할 수 있습니다. 리스트에 항목을 추가하더라도 기존 이터레이터는 유효한 상태를 유지합니다. 리스트에서 항목을 제거하면, 제거된 항목을 가리키는 이터레이터는 매달린 이터레이터가 됩니다.

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

QLinkedList::const_iterator 및 QMutableLinkedListIterator도 참조하십시오 .

멤버 함수 문서

iterator::iterator()

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

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

QLinkedList::begin() 및 QLinkedList::end()도 참조하십시오 .

bool iterator::operator!=(const QLinkedList<T>::const_iterator &other) const

bool iterator::operator!=(const QLinkedList<T>::iterator &other) const

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

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

T &iterator::operator*() const

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

할당문의 왼쪽에서 연산자 *()를 사용하여 항목의 값을 변경할 수 있습니다. 예를 들어:

if (*it == "Hello")
    *it = "Bonjour";

operator->()도 참조하십시오 .

QLinkedList<T>::iterator iterator::operator+(int j) const

이 이터레이터로부터 앞으로 j 위치에 있는 항목으로 이동하는 이터레이터를 반환합니다. ( j 가 음수인 경우, 이터레이터는 뒤로 이동합니다.)

j 값이 크면 이 연산의 수행 속도가 느려질 수 있습니다.

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

QLinkedList<T>::iterator &iterator::operator++()

접두사 ++ 연산자(++it)는 이터레이터를 목록의 다음 항목으로 이동시키고, 새로운 현재 항목을 가리키는 이터레이터를 반환합니다.

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

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

QLinkedList<T>::iterator iterator::operator++(int)

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

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

QLinkedList<T>::iterator &iterator::operator+=(int j)

이테레이터를 j 개 항목만큼 앞으로 이동시킵니다. ( j 가 음수인 경우, 이테레이터는 뒤로 이동합니다.)

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

QLinkedList<T>::iterator iterator::operator-(int j) const

이 이터레이터로부터 뒤쪽으로 j 위치에 있는 항목에 대한 이터레이터를 반환합니다. ( j 가 음수인 경우, 이터레이터는 앞으로 이동합니다.)

j 값이 클 경우 이 연산의 수행 속도가 느려질 수 있습니다.

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

QLinkedList<T>::iterator &iterator::operator--()

접두사 연산자(--it)는 앞의 항목을 현재 항목으로 설정하고, 새로운 현재 항목에 대한 이터레이터를 반환합니다.

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

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

QLinkedList<T>::iterator iterator::operator--(int)

포스트픽스 – 연산자(it--)는 바로 앞의 항목을 현재 항목으로 지정하고, 이전에 현재 항목이었던 항목에 대한 이터레이터를 반환합니다.

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

QLinkedList<T>::iterator &iterator::operator-=(int j)

이테레이터를 j 개 항목만큼 뒤로 이동시킵니다. ( j 가 음수인 경우, 이테레이터는 앞으로 이동합니다.)

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

T *iterator::operator->() const

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

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

bool iterator::operator==(const QLinkedList<T>::const_iterator &other) const

bool iterator::operator==(const QLinkedList<T>::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.