LearnFrenzy
Sign in Take a free test

STL Containers · STL Containers MCQ · Question 7

What does map[key] do for a missing key? Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

m is a std::map<std::string, int> without the key "x". What does int v = m["x"]; do?

  • A Throws std::out_of_range
  • B Inserts "x" with value 0 and returns 0 Correct
  • C Returns garbage
  • D Does not compile
Correct Answer: Option B

at() throws instead; find() and contains() look up without inserting.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links