このページでは

QSet::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 を変更する必要がない限り、非 constQSet に対してもQSet::const_iterator を使用するのが一般的なベストプラクティスです。const イテレータはわずかに高速であり、コードの可読性を向上させることができます。

デフォルトのQSet::iterator コンストラクタは、初期化されていないイテレータを作成します。反復処理を開始する前に、QSet::begin()、QSet::end()、またはQSet::insert() などの関数を使用して、イテレータを初期化する必要があります。以下は、セットに格納されているすべての項目を出力する典型的なループです:

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

// i は QSet<QString>::iterator です
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 のコピーを作成します。

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

const T &iterator::operator*() const

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

operator->()も参照してください 。

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

プレフィックス++演算子(++it )は、イテレータをセット内の次の要素へと進め、新しい現在の要素へのイテレータを返します。

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

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

後置の++演算子(it++ )は、イテレータをセット内の次の要素に進め、直前の要素へのイテレータを返します。

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

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

operator*()も参照してください 。

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

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

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