Map count in C++

Level 3102 keystrokes

map::count returns how many entries have the given key. Keys in a map are unique, so the result is 1 if the key exists and 0 if not: "a" gives 1 and "c" gives 0.

C++ code

1map<string, int> m = {{"a", 1}, {"b", 2}};
// "a" is a key: prints 1
2cout << m.count("a") << endl;
// "c" is not: prints 0; unlike m["c"], count does not insert the key
3cout << m.count("c") << endl;

Output

1
0
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
  • .× 2
  • (× 2
  • )× 2
  • =× 1

More C++ examples at level 3

← C++ typing practice