キーを指定してQHashから要素を削除
解説
remove()はQHashの指定したキーの要素を削除します.1つのキーに対して複数の値が関連づけられ手いる場合はその全てが削除されます.
定義は以下の通りです.
int QHash::remove ( const Key & key )
remove()の引数keyには削除する要素のキーを指定します.
削除した数を返します.
使用例
#include <QTextCodec>
#include <QTextStream>
#include <QHash>
int main(int argc, char *argv[]) {
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QTextStream out(stdout);
QHash<QString, int> hash;
hash.insert("a", 0);
hash.insert("b", 1);
hash.insertMulti ("b", 2); // hash = {<a, 0>, <b, 1>, <b, 2>}
out << hash.size() << "\n"; // 3
hash.
remove("a"); // hash = {<b, 1>, <b, 2>} out << hash.size() << "\n"; // 2
hash.
remove("b"); // hash = {} out << hash.size() << "\n"; // 0
return 0;
}
出力
3
2
0
最終更新:2011年09月23日 19:20