So, I have this situation where I need to see if an object is in my stl map. If it isn't, I am going to add it.
char symbolName[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
map<string,TheObject> theMap;
if (theMap.find(symbolName)==theMap.end()) {
TheObject theObject(symbolName);
theMap.insert(pair<string, TheObject>(symbolName,
theObject));
}
I am getting a core dump on the: theMap.find when the object is not already in the map. Supposedly, if the item is not in the map, it is supposed to return an iterator equivelent to map::end
What is going on here?
GCC: 3.4.6
It can crash because of many reasons. Without knowing the definition of at least TheObject
's constructors, i think we are largely left to guess at the problem. So far, your code looks fine, but it can be simplified:
char symbolName[] = "Hello";
map<string,TheObject> theMap;
theMap.insert(make_pair(symbolName, TheObject(symbolName)));
It won't do anything if the symbol is already mapped, discarding the new TheObject object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With