QHash::const_iterator Class
class QHash::const_iteratorQHash::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 を変更する必要がない限り、非constのQHash に対してもQHash::const_iterator を使用するのが一般的なベストプラクティスです。constイテレータはわずかに高速であり、コードの可読性を向上させることができます。
QHash::const_iterator のデフォルトコンストラクタは、初期化されていないイテレータを作成します。反復処理を開始する前に、QHash::cbegin()、QHash::cend()、またはQHash::constFind()などの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 は項目を任意の順序で格納します。唯一保証されるのは、同じキーを持つ項目(QMultiHash を使用して挿入されたもの)が、最も新しく挿入された値から最も古い値へと連続して表示されるという点です。
同じハッシュに対して複数のイテレータを使用することができます。ただし、QHash に対して直接行われる変更(項目の挿入や削除)により、イテレータが無効になる可能性があることに注意してください。
ハッシュへの項目の挿入、あるいはQHash::reserve() やQHash::squeeze() などのメソッドの呼び出しは、そのハッシュを指すすべてのイテレータを無効にする可能性があります。 イテレータの有効性が保証されるのは、QHash が内部のハッシュテーブルを拡大・縮小する必要がない場合に限られます。再ハッシュ操作が行われた後にイテレータを使用すると、未定義の挙動を引き起こすことになります。
ただし、QHash::erase() メソッドを使用してハッシュからエントリを削除する場合、イテレータを安全に使用できます。この関数は反復処理中に安全に呼び出すことができ、ハッシュ内の項目の順序に影響を与えません。
警告: 暗黙的に共有されるコンテナ上のイテレータは 、STLのイテレータとまったく同じように動作するわけではありません。コンテナに対してイテレータがアクティブな間は、そのコンテナのコピーを避けるべきです。詳細については、「暗黙的な共有によるイテレータの問題」を参照してください。
関連項目: QHash::iterator 、QHash::key_iterator 、およびQHash::const_key_value_iterator 。
メンバ関数のドキュメント
[constexpr noexcept default] 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!=()も参照してください 。
© 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.