Dave's Brain

Browse - Programming Tips - What happens when a key is missing from a std::map ?

Date: 2015apr24
Language: C++

Q.  What happens when a key is missing from a std::map<> ?

A.  It makes an empty entry then gives that to you.
If we have:

std::map<std::string, std::string> fruit; fruit["apple"] = "red"; fruit["bannana"] = "yellow";
Then we try to get:
std::string c = fruit["orange"];
String c will be "". Since its been added to the map. Note this as changed fruit.size() You can use fruit.find("orange") to see if a key is present.

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.