QHash > size

要素数を取得

解説

size(),count()はQHashの要素数を返します.

定義は以下の通りです.
int QHash::size () const
int QHash::count () const
int QHash::count ( const Key & key ) const
count()の引数keyを指定した場合,keyと一致する要素の数を数えます.

要素数が0かどうかの判定は,isEmpty()を用いて行うことができます.

使用例

  1. #include <QTextCodec>
  2. #include <QTextStream>
  3. #include <QHash>
  4. int main(int argc, char *argv[]) {
  5. QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
  6. QTextStream out(stdout);
  7.  
  8. QHash<QString, int> hash;
  9. hash.insert("a", 0);
  10. hash.insert("b", 1);
  11. hash.insertMulti ("b", 2); // hash = {<a, 0>, <b, 1>, <b, 2>}
  12. out << hash.size() << "\n"; // 3
  13. out << hash.count() << "\n";// 3
  14. out << hash.count("b") << "\n"; // 2
  15.  
  16. return 0;
  17. }

出力
3
3
2
最終更新:2011年09月23日 19:59
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。