iterator Class

class QMultiHash::iterator

QMultiHash::iterator クラスは、QMultiHash 用の STL スタイルの非 const イテレータを提供します。詳細...

パブリック関数

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

詳細説明

QMultiHash<Key, T>::iterator を使用すると、QMultiHash を繰り返し処理したり、特定のキーに関連付けられた値 (キーではない) を変更したりすることができます。constQMultiHash を反復処理したい場合は、QMultiHash::const_iterator を使用する必要があります。イテレータを使用してQMultiHash を変更する必要がない限り、constQMultiHash 以外でもQMultiHash::const_iterator を使用するのが一般的です。const イテレータのほうが若干高速で、コードの可読性も向上します。

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

QHash<QString, int> hash;
hash.insert("January", 1);
hash.insert("February", 2);
...
hash.insert("December", 12);

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

項目をキー順に並べるQMap とは異なり、QMultiHash は任意の順序で項目を格納する。

以下は、QMultiHash に格納されているすべての値を2ずつインクリメントする例である:

for (auto i = hash.begin(), end = hash.end(); i != end; ++i)
    i.value() += 2;

QMultiHash から要素を削除するには、erase_if(QMultiHash<Key, T> &map, Predicate pred)を使います:

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

複数のイテレータを同じハッシュに使用することができます。しかし、QHash (項目の挿入や削除)に対して直接行われる修正は、イテレータを無効にしてしまう可能性があることに注意してください。

ハッシュに項目を挿入したり、QHash::reserve() やQHash::squeeze() などのメソッドを呼び出したりすると、ハッシュを指すすべてのイテレータが無効になる可能性があります。イテレータは、QHash がその内部ハッシュ・テーブルを拡大/縮小する必要がない限り、有効であり続けることが保証される。再ハッシュ操作が発生した後にイテレータを使用すると、未定義の動作につながります。

長期間イテレータを保持する必要がある場合は、QHash ではなくQMultiMap を使用することをお勧めします。

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

QMultiHash::const_iteratorQMultiHash::key_iteratorQMultiHash::key_value_iteratorも参照のこと

メンバ関数ドキュメント

[noexcept] bool iterator::operator==(const QMultiHash<Key, T>::const_iterator &other) const

[noexcept] bool iterator::operator==(const QMultiHash<Key, T>::iterator &other) const

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

operator!=()も参照

[noexcept] bool iterator::operator!=(const QMultiHash<Key, T>::const_iterator &other) const

[noexcept] bool iterator::operator!=(const QMultiHash<Key, T>::iterator &other) const

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

operator==()も参照

[constexpr noexcept] iterator::iterator()

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

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

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

[noexcept] const Key &iterator::key() const

現在のアイテムのキーを const リファレンスとして返します。

QMultiHash::erase() の後にQMultiHash::insert() を呼び出すことで可能ですが、イテレータを通して項目のキーを変更する直接的な方法はありません。

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

[noexcept] T &iterator::value() const

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

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

if (i.key() == "Hello")
    i.value() = "Bonjour";

key() およびoperator*()も参照

[noexcept] T &iterator::operator*() const

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

value() と同じ。

key()も参照

[noexcept] QMultiHash<Key, T>::iterator &iterator::operator++()

接頭辞 ++ 演算子 (++i) は、イテレータをハッシュの次の項目に進め、新しい現在の項目へのイテレータを返します。

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

[noexcept] QMultiHash<Key, T>::iterator iterator::operator++(int)

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

ポストフィックス ++ 演算子 (i++) は、イテレータをハッシュの次の項目に進め、それ以前の現在の項目へのイテレータを返します。

[noexcept] T *iterator::operator->() const

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

value()も参照

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