QHash > unite

QHashを結合

解説

uniqueKeys()はQHash同士を結合します.キーが重複する場合は1つのキーに対して複数の値が設定されます.

定義は以下の通りです.
QHash<Key, T> & QHash::unite ( const QHash<Key, T> & other )
引数otherには連結したいQHashを指定します.

使用例

  1. #include <QTextCodec>
  2. #include <QTextStream>
  3. #include <QHash>
  4. #include <QList>
  5. #include <QHashIterator>
  6.  
  7. int main(int argc, char *argv[]) {
  8. QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
  9. QTextStream out(stdout);
  10.  
  11. QHash<QString, int> hash1, hash2;
  12. hash1.insert("a", 0); // hash1 = {<a, 0>}
  13. hash1.insert("b", 1); // hash1 = {<a, 0>, <b, 1>}
  14. hash2.insert("a", 0); // hash2 = {<a, 0>}
  15. hash2.insert("c", 2); // hash2 = {<a, 0>, <c, 2>}
  16. hash2.unite(hash1); // hash2 = {<a, 0>, <a, 0>, <b, 1>, <c, 2>}
  17.  
  18. QHashIterator<QString, int> it(hash2);
  19. while (it.hasNext()) {
  20. it.next();
  21. out << "<" << it.key() << ", " << it.value() << ">, ";
  22. }
  23. out << "\n";
  24.  
  25. return 0;
  26. }

出力
<a, 0>, <a, 0>, <b, 1>, <c, 2>,
最終更新:2011年09月23日 19:46
ツールボックス

下から選んでください:

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