Unordered map in C++

Level 3130 keystrokes

unordered_map stores key-value pairs in a hash table, giving fast lookups on average but no defined order. Prints Alice's value (25) and the number of entries (2).

C++ code

// Like map, but hash-based: keys are not kept in sorted order
1unordered_map<string, int> ages;
// [] inserts the key if it is missing and assigns the value
2ages["Alice"] = 25;
3ages["Bob"] = 30;
// Average constant-time lookup: prints 25
4cout << ages["Alice"] << endl;
// Two entries: prints 2
5cout << ages.size() << endl;

Output

25
2
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
  • "× 6
  • ;× 5
  • [× 3
  • ]× 3
  • =× 2
  • _× 1
  • ,× 1
  • .× 1
  • (× 1

More C++ examples at level 3

← C++ typing practice