このページでは

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> を反復処理し、そのイテレータに関連付けられたリストの項目を変更することができます。 constQLinkedList を反復処理したい場合は、代わりにQLinkedList::const_iterator を使用してください。イテレータを介してQLinkedList を変更する必要がない限り、非 constQLinkedList に対しても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;
}

しかし、これは++i でクラッシュする可能性があります。なぜなら、erase()の呼び出し後、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.