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> を反復処理し、イテレータに関連付けられたリスト項目を変更することができます。QLinkedList を反復処理したい場合は、代わりにQLinkedList::const_iterator を使用してください。イテレータを通してQLinkedList を変更する必要がない限り、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::iterator QLinkedList::const_iterator 。 <int>に格納されているすべての値を2ずつインクリメントする例です:QLinkedList

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 でクラッシュする可能性があります。なぜなら、i は、erase() の呼び出しの後でダングリング・イテレータだからです。

同じリストで複数のイテレータを使用することができます。リストに項目を追加しても、既存のイテレータは有効なままです。リストから項目を削除すると、削除された項目を指すイテレータはダングリング・イテレータになります。

警告 暗黙的に共有されたコンテナ上のイテレータは、STLイテレータのようには動作しません。そのコンテナ上でイテレータがアクティブになっている間は、コンテナのコピーを避けるべきである。詳細については、暗黙の共有イテレータ問題を参照してください。

QLinkedList::const_iterator およびQMutableLinkedListIteratorも参照のこと

メンバ関数ドキュメント

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

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

other がこのイテレータと同じアイテムを指している場合はtrue を返し、そうでない場合はfalse を返す。

operator!=()も参照

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

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

other がこのイテレータとは異なるアイテムを指している場合はtrue を返し、そうでない場合はfalse を返す。

operator==()も参照

iterator::iterator()

初期化されていないイテレータを構築します。

operator*() や operator++() のような関数を、初期化されていないイテレータに対して呼び出してはいけません。operator=() を使用して値を代入してから使用してください。

QLinkedList::begin() およびQLinkedList::end()も参照してください

T &iterator::operator*() const

現在の項目への変更可能な参照を返します。

例えば、代入の左辺で operator*() を使用することで、項目の値を変更することができます:

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)

これはオーバーロードされた関数である。

postfix ++ 演算子 (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)

これはオーバーロードされた関数である。

postfix - 演算子 (it--) は、直前の項目をカレントにし、直前の項目へのイテレータを返します。

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

イテレータをj アイテム分戻します。(j が負の場合、イテレータは前に進む)。

operator+=() およびoperator-()も参照のこと

T *iterator::operator->() const

現在の項目へのポインタを返します。

operator*()も参照

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