このページでは

QMap::const_iterator Class

class QMap::const_iterator

QMap::const_iterator クラスは、QMap に対してSTLスタイルのconstイテレータを提供します。詳細...

パブリック型

パブリック関数

const_iterator()
const_iterator(const QMap<Key, T>::iterator &other)
const Key &key() const
const T &value() const
const T &operator*() const
QMap<Key, T>::const_iterator &operator++()
QMap<Key, T>::const_iterator operator++(int)
QMap<Key, T>::const_iterator &operator--()
QMap<Key, T>::const_iterator operator--(int)
const T *operator->() const
bool operator!=(const QMap<Key, T>::const_iterator &lhs, const QMap<Key, T>::const_iterator &rhs)
bool operator==(const QMap<Key, T>::const_iterator &lhs, const QMap<Key, T>::const_iterator &rhs)

詳細な説明

QMap<Key, T>::const_iterator を使用すると、QMap を反復処理できます。反復処理中にQMap を変更したい場合は、代わりにQMap::iterator を使用する必要があります。 イテレータを介してQMap を変更する必要がない限り、非constのQMap に対してもQMap::const_iterator を使用するのが一般的なベストプラクティスです。constイテレータはわずかに高速であり、コードの可読性を向上させることができます。

デフォルトのQMap::const_iterator コンストラクタは、初期化されていないイテレータを作成します。反復処理を開始する前に、QMap::cbegin()、QMap::cend()、またはQMap::constFind()などのQMap 関数を使用して、イテレータを初期化する必要があります。以下は、マップに格納されているすべての(key, value)ペアを出力する典型的なループです:

QMap<QString, int> map;
map.insert("January", 1);
map.insert("February", 2);
//...
map.insert("December", 12);

for (auto i = map.cbegin(), end = map.cend(); i != end; ++i)
    cout << qPrintable(i.key()) << ": " << i.value() << endl;

以下は、値が 10 より大きいすべての項目を削除する例です:

QMap<QString, int>::const_iterator i = map.cbegin();
while (i != map.cend()) {
    if (i.value() > 10)
        i = map.erase(i);
    else
        ++i;
}

また、erase_if() を使用した場合の同様の動作は以下の通りです。

erase_if(map, [](const QMap<QString, int>::iterator it) { return it.value() > 10; });

QHash は項目を任意の順序で格納するのに対し、QMap は項目をキー順に格納します。

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

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

関連項目: QMap::iterator 、QMap::key_iterator 、およびQMap::const_key_value_iterator 。

メンバ型のドキュメント

[alias] const_iterator::iterator_category

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

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

[default] const_iterator::const_iterator()

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

key()、value()、およびoperator++() などの関数は、初期化されていないイテレータに対して呼び出してはなりません。使用する前に、operator=() を使用して値を代入してください。

QMap::constBegin() およびQMap::constEnd()も参照してください 。

const_iterator::const_iterator(const QMap<Key, T>::iterator &other)

other のコピーを作成します。

const Key &const_iterator::key() const

現在の項目のキーを返します。

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

const T &const_iterator::value() const

現在の項目の値を返します。

key() およびoperator*()も参照してください 。

const T &const_iterator::operator*() const

現在の項目の値を返します。

value() と同様です。

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

QMap<Key, T>::const_iterator &const_iterator::operator++()

プレフィックス演算子「++ 」(++i )は、イテレータをマップ内の次の項目へと進め、新しい現在の項目へのイテレータを返します。

QMap::end() に対してこの関数を呼び出すと、未定義の結果となります。

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

QMap<Key, T>::const_iterator const_iterator::operator++(int)

後置演算子++ (i++ )は、イテレータをマップ内の次の要素へと進め、直前に現在要素だった要素へのイテレータを返します。

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

QMap<Key, T>::const_iterator &const_iterator::operator--()

プレフィックス演算子 `-- ` (--i) は、その直前の要素を現在の要素として設定し、新しい現在の要素を指すイテレータを返します。

QMap::begin() に対してこの関数を呼び出すと、未定義の結果となります。

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

QMap<Key, T>::const_iterator const_iterator::operator--(int)

後置演算子 `-- ` (i--) は、直前の要素を現在の要素とし、直前に現在の要素だった要素を指すイテレータを返します。

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

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

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

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

関連する非メンバー関数

bool operator!=(const QMap<Key, T>::const_iterator &lhs, const QMap<Key, T>::const_iterator &rhs)

lhs がrhs イテレータとは異なる項目を指している場合はtrue を返し、そうでない場合はfalse を返します。

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

bool operator==(const QMap<Key, T>::const_iterator &lhs, const QMap<Key, T>::const_iterator &rhs)

lhs がrhs のイテレータと同じ項目を指している場合は `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.