iterator Class

class QSet::iterator

QSet::iterator クラスは、QSet 用の STL スタイルの非 const イテレータを提供します。さらに...

パブリック型

パブリック関数

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

詳細説明

QSet には、STLスタイルのイテレータと Javaスタイルのイテレータの両方が用意されている。STLスタイルのイテレータはより低レベルで使い方が面倒だが、その反面、若干高速で、すでにSTLを知っている開発者にとっては親しみやすいという利点がある。

QSet<T>::iterator を使うと、QSet を繰り返し処理することができ、繰り返し処理中にアイテムを削除することもできます(QSet::erase() を使用)。(QSet では、イテレータを通して値を変更することはできません。これは、QSet で使用される内部ハッシュテーブルの値を移動する必要がある可能性があるためです)。constQSet を反復処理したい場合は、QSet::const_iterator を使用する必要があります。イテレータを通してQSet を変更する必要がない限り、constでないQSet に対してもQSet::const_iterator を使用するのが一般的です。constイテレータの方が若干高速で、コードの可読性も向上します。

デフォルトのQSet::iterator コンストラクタは、初期化されていないイテレータを作成します。QSet::begin ()、QSet::end ()、QSet::insert ()のような関数を使用して初期化しなければなりません。以下は、セットに格納されているすべての項目を表示する典型的なループです:

QSet<QString> set={"January", "February", ... "December"}// iはQSet<QString>::イテレータfor(autoi=set.begin(),end=set.end(); i!=end;++i)    qDebug() << *i;

以下は、反復処理中にセットから特定のアイテム('J'で始まるものすべて)を削除するループである:

QSet<QString> set = {"January", "February", ... "December"};

auto i = set.begin();
while (i != set.end()) {
    if ((*i).startsWith('J')) {
        i = set.erase(i);
    } else {
        ++i;
    }
}

STLスタイルのイテレータは、generic algorithms の引数として使用できる。例えば、qFind() アルゴリズムを使用してセット内の項目を見つける方法です:

QSet<QString> set;
...
const auto predicate = [](const QString &s) { return s.compare("Jeanette", Qt::CaseInsensitive) == 0; };
QSet<QString>::iterator it = std::find_if(set.begin(), set.end(), predicate);
if (it != set.end())
    cout << "Found Jeanette" << endl;

同じセットに対して複数の反復子を使用できます。

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

QSet::const_iterator およびQMutableSetIteratorも参照のこと

メンバ型ドキュメント

iterator::iterator_category

これらのイテレータが双方向イテレータであることを示すstd::bidirectional_iterator_tagの同義語。

メンバ関数のドキュメント

iterator::iterator()

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

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

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

iterator::iterator(const QSet<T>::iterator &other)

other のコピーを作成する。

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

このイテレータにother を割り当てます。

const T &iterator::operator*() const

現在の項目への参照を返します。

operator->()も参照

const T *iterator::operator->() const

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

operator*()も参照

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

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

operator!=()も参照してください

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

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

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

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

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

operator==()も参照

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

接頭辞 ++ 演算子 (++it) は、イテレータをセットの次のアイテムに進め、新しい現在のアイテムへのイテレータを返します。

QSet<T>::constEnd() でこの関数を呼び出すと、未定義の結果になります。

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

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

ポストフィックス ++ 演算子 (it++) は、イテレータをセットの次のアイテムに進め、それ以前の現在のアイテムへのイテレータを返します。

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