iterator Class
class QHash::iteratorQHash::iterator クラスは、QHash 用の STL スタイルの非 const イテレータを提供します。詳細...
パブリック関数
iterator() | |
const Key & | key() const |
T & | value() const |
bool | operator!=(const QHash<Key, T>::const_iterator &other) const |
bool | operator!=(const QHash<Key, T>::iterator &other) const |
T & | operator*() const |
QHash<Key, T>::iterator & | operator++() |
QHash<Key, T>::iterator | operator++(int) |
T * | operator->() const |
bool | operator==(const QHash<Key, T>::const_iterator &other) const |
bool | operator==(const QHash<Key, T>::iterator &other) const |
詳しい説明
QHash<Key, T>::iterator を使用すると、QHash を繰り返し処理したり、特定のキーに関連付けられた値 (キーではない) を変更したりすることができます。constQHash を反復処理したい場合は、QHash::const_iterator を使用する必要があります。イテレータを使用してQHash を変更する必要がない限り、constQHash 以外でもQHash::const_iterator を使用するのが一般的です。const イテレータのほうが若干高速で、コードの可読性も向上します。
デフォルトのQHash::iterator コンストラクタは、初期化されていないイテレータを作成します。QHash::begin ()、QHash::end ()、QHash::find ()のような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(key()) << ": " << i.value() << endl;
項目をキー順に並べるQMap とは異なり、QHash は任意の順序で項目を格納する。
以下は、QHash に格納されているすべての値を2ずつインクリメントする例である:
for (auto i = hash.begin(), end = hash.end(); i != end; ++i) i.value() += 2;
QHash から要素を削除するには、erase_if(QHash<Key, T> &map, Predicate pred)を使います:
複数のイテレータを同じハッシュに使用することができます。しかし、QHash (項目の挿入や削除)に対して直接行われる修正は、イテレータを無効にしてしまう可能性があることに注意してください。
ハッシュに項目を挿入したり、QHash::reserve() やQHash::squeeze() などのメソッドを呼び出したりすると、ハッシュを指すすべてのイテレータが無効になる可能性があります。イテレータは、QHash がその内部ハッシュ・テーブルを拡大/縮小する必要がない限り、有効であり続けることが保証される。再ハッシュ操作が発生した後にイテレータを使用すると、未定義の動作につながります。
長期間イテレータを保持する必要がある場合は、QHash ではなくQMap を使用することをお勧めします。
警告: 暗黙的に共有されたコンテナ上のイテレータは、STLイテレータのようには動作しません。そのコンテナ上でイテレータがアクティブになっている間は、コンテナのコピーを避けるべきである。詳細については、暗黙的共有イテレータ問題を参照してください。
QHash::const_iterator 、QHash::key_iterator 、QHash::key_value_iteratorも参照のこと 。
メンバ関数ドキュメント
[noexcept]
bool iterator::operator==(const QHash<Key, T>::const_iterator &other) const
[noexcept]
bool iterator::operator==(const QHash<Key, T>::iterator &other) const
other がこのイテレータと同じアイテムを指している場合はtrue
を返し、そうでない場合はfalse
を返す。
operator!=()も参照してください 。
[noexcept]
bool iterator::operator!=(const QHash<Key, T>::const_iterator &other) const
[noexcept]
bool iterator::operator!=(const QHash<Key, T>::iterator &other) const
other がこのイテレータとは異なるアイテムを指している場合はtrue
を返し、そうでない場合はfalse
を返す。
operator==()も参照 。
[constexpr noexcept]
iterator::iterator()
初期化されていないイテレータを構築します。
key()、value()、 operator++() などの関数を、初期化されていないイテレータで呼び出してはいけません。operator=() を使用して値を代入してから使用してください。
QHash::begin() およびQHash::end()も参照してください 。
[noexcept]
const Key &iterator::key() const
現在のアイテムのキーを const リファレンスとして返します。
アイテムのキーをイテレータで直接変更する方法はありませんが、QHash::erase() の後にQHash::insert() を呼び出すことで変更できます。
value()も参照 。
[noexcept]
T &iterator::value() const
現在の項目の値への変更可能な参照を返します。
例えば、代入の左辺で value() を使用することで、項目の値を変更することができます:
if (i.key() == "Hello") i.value() = "Bonjour";
[noexcept]
T &iterator::operator*() const
現在の項目の値への変更可能な参照を返します。
value() と同じ。
key()も参照 。
[noexcept]
QHash<Key, T>::iterator &iterator::operator++()
接頭辞 ++ 演算子 (++i
) は、イテレータをハッシュの次の項目に進め、新しい現在の項目へのイテレータを返します。
この関数をQHash::end() で呼び出すと、未定義の結果になります。
[noexcept]
QHash<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.