const_iterator Class

class QHash::const_iterator

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

パブリック関数

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

詳しい説明

QHash<Key, T>::const_iterator を使用すると、QHash を繰り返し処理できます。QHash を繰り返し処理しながら変更したい場合は、代わりにQHash::iterator を使用する必要があります。イテレータを通してQHash を変更する必要がない限り、constQHash 以外でもQHash::const_iterator を使用するのが一般的です。constイテレータの方が若干高速で、コードの可読性も向上します。

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

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 、項目をキー順に並べるのとは異なり、QHash 、項目は任意の順序で格納される。唯一の保証は、同じキーを共有する項目は(QMultiHash を使って挿入されたため)、最近挿入された値から最近挿入されなかった値まで、連続して表示されるということである。

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

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

しかし、QHash::erase() メソッドを使用することで、安全にイテレータを使用してハッシュからエントリを削除することができます。この関数はイテレート中に安全にコールすることができ、ハッシュ内の項目の順序に影響を与えません。

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

QHash::iteratorQHash::key_iteratorQHash::const_key_value_iteratorも参照のこと

メンバ関数ドキュメント

[constexpr noexcept] const_iterator::const_iterator()

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

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

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

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

other のコピーを作成する。

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

現在のアイテムのキーを返します。

value()も参照

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

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

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

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

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

operator==()も参照

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

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

value() と同じ。

key()も参照

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

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

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

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

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

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

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

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

value()も参照

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

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

operator!=()も参照

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