QHash::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 関数を使用して、イテレータを初期化する必要があります。以下は、ハッシュに格納されているすべての(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 は要素をキー順に並べ替えますが、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 。
メンバ関数のドキュメント
[constexpr noexcept default] 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";key() および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==()も参照してください 。
[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()も参照してください 。
[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!=()も参照してください 。
© 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.