STL map.find problem in using a compare class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hagdanan
    New Member
    • Apr 2010
    • 3

    STL map.find problem in using a compare class

    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:
    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;
    }
    Last edited by Banfa; Apr 28 '10, 08:26 AM. Reason: Added [code]...[/code] tags
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    Comparison operator for map is '<' - strictly less. It checks equality as !(A<B) && !(B<A). So this
    # if( num1 <= num2 )
    # return true;
    should be num1 < num2.

    btw, my cygwin compiler doesn't like _B and thinks it's an integer constant.
    ctype.h : #define _B 0200

    Comment

    • hype261
      New Member
      • Apr 2010
      • 207

      #3
      Why are you using map&lt;string, string&gt;

      Why are you using std::map<std::s tring, std::string>? Since all your keys are integers it would be easier to use std::map<int, std::string> and it would be quicker also.

      Comment

      • hagdanan
        New Member
        • Apr 2010
        • 3

        #4
        Originally posted by hype261
        Why are you using std::map<std::s tring, std::string>? Since all your keys are integers it would be easier to use std::map<int, std::string> and it would be quicker also.
        Because sometimes we use keys that are not in integer form. And map do sort the keys when we insert on it, so if I use keys as "1", "2", "3", "10".. this will not be arrange numerically(sin ce its string) as expected to be 1, 2, 3 and 10.. but this will be arranged alphabetically as "1", "10","2","3 ". That's why I have this class to check if the string can be converted to integer and compare as integer so I can have the correct sequence.

        Comment

        • hagdanan
          New Member
          • Apr 2010
          • 3

          #5
          Originally posted by newb16
          Comparison operator for map is '<' - strictly less. It checks equality as !(A<B) && !(B<A). So this
          # if( num1 <= num2 )
          # return true;
          should be num1 < num2.

          btw, my cygwin compiler doesn't like _B and thinks it's an integer constant.
          ctype.h : #define _B 0200
          Thanks a lot.

          but I am wondering about the effect of the Sort class on the find method. I need to return false so that find will work?

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            You need to return true only if A is less than B. If they are equal return false.

            Comment

            Working...