Skip to main content
C++ Output Prediction Medium +20 XP

Map Insertion

What is printed?

C++Read-only
map<string, int> m;
m["a"] = 1;
m["b"] = 2;
m["a"] = 3;
cout << m["a"] << " " << m.size();

What is the output?

Show answer and explanation

Answer: 3 2

m['a'] is first set to 1, then overwritten to 3. Size is 2 because only 'a' and 'b' are keys.

Hint: Duplicate keys overwrite values.

What this challenge tests

Output Prediction in C++: map, stl.

Related challenges