Hi all, I am new to STL and I have a problem with my map. I made a class(Sort) for comparison on the map but the problem is map.find will not work.
Can someone explain why find was not able to locate the key?
here is the code:
Can someone explain why find was not able to locate the key?
here is the code:
Code:
#include<iostream> #include<map> #include<string> using namespace std; class Sort { public: bool operator() (string const &_A, string const &_B) const { double num1, num2; num1 = atoi(_A.c_str()); num2 = atoi(_B.c_str()); if( num1 && num2 ) { if( num1 <= num2 ) return true; else return false; } else { if( _A.compare(_B) <= 0 ) return true; else return false; } } }; typedef map<string, string, Sort> EnumMap; int main() { string str="A"; EnumMap months; EnumMap::iterator it; months["1"] = "January"; months["7"] = "July"; months["2"] = "February"; months["3"] = "March"; months["4"] = "April"; months["5"] = "May"; months["6"] = "June"; months["8"] = "August"; months["9"] = "September"; months["10"] = "October"; months["11"] = "November"; months["12"] = "December"; for ( it=months.begin() ; it!=months.end(); it++ ) { cout << (*it).first << "==>" << (*it).second << endl; } EnumMap::iterator t2= months.find("7"); if(t2!=months.end()) cout << "Found" << endl; else cout << "Not Found" << endl; }
Comment