c++ - The key exist in the map variable but find cannot find it -
i have created class named myclass
, define map as:
map<string,myclass> myclasssample;
i inserted variable , key:
myclasssample["id"].setstring1_1("hi");
note: setstring1_1
setter of class
then use code see if key available:
if (myclasssample.find("id") != myclasssample.end()) { printf("problem"); }
problem
shown out put. condition true! function can return string!
return myclasssample["id"].getstring1();
note: getstring1
getter of class
you seem have misunderstanding of how map::find
works. returns end()
iterator when key you're searching cannot found. condition check if key present needs be
if (myclasssample.find("id") == myclasssample.end()) // ^^ ==, not != { printf("problem"); }
Comments
Post a Comment