In my code, I have a std::map
that looks something like this:
std::map<std::string, A*> myMap;
where A is one of my custom classes.
When I access a map element which does not exist via operator[]
like so:
std::string s("hello");
A* pA = myMap[s];
I know that a new element will be created with that key but I would like the pointer to be initialized to NULL. In other words, if myMap[s]
exists, a valid pointer should be returned. If not, I would like pA to be NULL after the above code executes.
By default, will pA contain garbage if myMap[s]
doesn't exit? How can I make it so that pA contains the value NULL if the element doesn't exit?