このページでは

QMultiHash::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 関数を使用して、イテレータを初期化する必要があります。以下は、ハッシュに格納されているすべての(key, value)ペアを出力する典型的なループです:

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(i.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_iterator 、QMultiHash::key_iterator 、およびQMultiHash::key_value_iterator 。

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

[constexpr noexcept default] 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] 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] 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()も参照してください 。

[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!=()も参照してください 。

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