Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> m{{"a", 1}};
int b = m["b"];
std::cout << b << ' ' << m.size() << '\n';
}0 2
- Expecting
0 1:m["b"]inserts the missing key"b"before it returns the value, so the size grows to2. - Expecting undefined behavior: the inserted value is value-initialized, which makes an
int0, as inint x{};.
- How do you check whether
mhas the key"b"without inserting it? - Why does
m["b"]not compile whenmis aconst std::map?