c++ map find problem

Jul 14, 2011 at 11:15am
Ok, i have a map hash (string, string)

for(map<string, string>::iterator it = hash.begin(); it != hash.end(); it++)
log_file("first: %s\n", (*it).first.c_str()); // "first_elem" iterated

when i do
map<string,string>::iterator it = hash.find("first_elem");
if (it != hash.end())
log_file("found!\n");
else
log_file("not found!\n");

into log out "not found!"
why???

Jul 14, 2011 at 11:26am
Well from what you have posted here hash is empty. So you can't find anything that is not in the hash right?
Jul 14, 2011 at 12:55pm
Can you share the code where in you insert "first_elem" in this map. I mean share the whole code to avoid confusion.
Jul 14, 2011 at 9:16pm
if (key != std::string::npos)
{
std::string keyname = sec;
keyname+=":";
keyname += str.substr(0, key);
std::string val = str.substr(key+1, str.size()-key);

mHash.insert(std::make_pair<std::string, std::string>(keyname, val));

FILE *f = fopen("elems.txt","a");
fprintf(f,"%s-%s\n",keyname.c_str(),val.c_str());
}

^ i logged every keyname and val
and i get

cfg:string - lol
cfg:bool - 1
cfg:int - 357
cfg:float - 35.057
cfg:double - 3.14159
server:name - xd

now the problem is here:

std::map<std::string, std::string>::iterator it = mHash.find("cfg:string");
if (it != mHash.end())
{
fprintf(f,"found!\n");
fclose(f);
}

found! isn't logged :(

Jul 16, 2011 at 1:28pm
Its not fully clear for me but I've got the impression that keyname contains a white space at the end (according your log output). It depends on what "key" points to.
Topic archived. No new replies allowed.