-2

I have a string which is x = 5 and i spliced it to x 5 then declare a map

 map<string, double> variableMap;

i put them into a map use

variableMap.insert(make_pair(splitString.at(0), stod(splitString.at(1))));

splitString.at(0) will be x and stod(splitString.at(1)) will be 5

but when i was trying to get the value from find key by using

                 map<string, double>::iterator iter;

                 iter = variableMap.find("x");

                     cout << iter->second<<endl;

it prints 6.95321e-310

this is really weird

J.Doe
  • 3
  • 1

1 Answers1

1

You likely got the splitString wrong, so that the map does not really contain the key "x". You should check the result of find for equality with variableMap.end() before use.

iter = variableMap.find("x");
if(iter == variableMap.end()) {
  // not found ...
}

Use a debuger, and verify that the pair has the right content, including white space for the string.

You could also print it to check what it contains:

for(auto elem : variableMap) {
   std::cout << ">"<<elem.first << "< " << elem.second << std::endl;
}

Edit: as your problem was indeed white space, consider not writing string string manipulations yourself, but instead look at the top answers at C++ - Split string by regex

Community
  • 1
  • 1
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
  • yea when i useif(iter != variableMap.end()) { cout<< iter->second; } it doesn't print any thing. so i guess you are right. key may not contain at the map. I'm checking if if string contain white space – J.Doe Nov 18 '15 at 23:10
  • yup, it is the matter of white space, i splitString wrong. thank you soooooo much!! – J.Doe Nov 18 '15 at 23:20