Unordered set in C++

Level 3133 keystrokes

unordered_set stores unique values in a hash table, so lookups are fast on average but the elements have no defined order. After inserting "bird" it has 3 elements, and count("cat") is 1.

C++ code

// A hash-based set: unique values, no guaranteed order
1unordered_set<string> words = {"cat", "dog"};
// Adds "bird"; inserting a value that already exists would change nothing
2words.insert("bird");
// cat, dog and bird: prints 3
3cout << words.size() << endl;
// count is 1 if the value is present and 0 if not: prints 1
4cout << words.count("cat") << endl;

Output

3
1
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 9
  • "× 8
  • ;× 4
  • .× 3
  • (× 3
  • )× 3
  • _× 1
  • ,× 1
  • {× 1
  • }× 1

More C++ examples at level 3

← C++ typing practice