このページでは

QMultiHash::const_iterator Class

class QMultiHash::const_iterator

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

パブリック関数

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

詳細な説明

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

QMultiHash::const_iterator のデフォルトコンストラクタは、初期化されていないイテレータを作成します。反復処理を開始する前に、QMultiHash::cbegin()、QMultiHash::cend()、またはQMultiHash::constFind()などの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 を使用して挿入されたもの)が、最も新しく挿入された値から最も古い値へと連続して表示されるという点です。

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

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

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

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

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

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

[constexpr noexcept default] const_iterator::const_iterator()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

value() と同じです。

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

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

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

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

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

後置++演算子(i++ )は、イテレータをハッシュ内の次の要素へと進め、直前の要素へのイテレータを返します。

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

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

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

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

[noexcept] bool const_iterator::operator==(const QMultiHash<Key, T>::const_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.