QHash > remove

キーを指定してQHashから要素を削除

解説

remove()はQHashの指定したキーの要素を削除します.1つのキーに対して複数の値が関連づけられ手いる場合はその全てが削除されます.

定義は以下の通りです.
int QHash::remove ( const Key & key )
remove()の引数keyには削除する要素のキーを指定します.
削除した数を返します.

使用例

  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.  
  14. hash.remove("a"); // hash = {<b, 1>, <b, 2>}
  15. out << hash.size() << "\n"; // 2
  16.  
  17. hash.remove("b"); // hash = {}
  18. out << hash.size() << "\n"; // 0
  19.  
  20. return 0;
  21. }

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

下から選んでください:

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