Under what circumstances does the [] operator for std :: map return 0?

I work with LLVM and I am having problems with the following piece of code that I did not write:

static std::map<std::string, Value*> NamedValues;
... //Lots of other code
Value *V = NamedValues["Demo string"];
return V ? V : ErrorV("V is not in NamedValues map.");

From what I understand in std :: map, it should never return a null pointer (unless it's from memory?), So it's hard for me to understand how V 0 indicates that V is not on the map. As with my program, an error always occurs here, but I canโ€™t understand why. Any help on what's going on here?

+4
source share
2 answers

std::map::operator [] , , , .

POD (, ) . , nullptr, .

"Demo string" NamedValues["Demo string"];, , nullptr.

, , find + end:

if (map.find(yourKey) != map.end()){
  //the key exists
}

EDIT:
@ShadowRanger, count .

if (map.count(yourKey)){
   //the key exists
}
+6

[] . , .

, , nullptr , , .

+2

All Articles