newbie question:why does this code work...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anagram
    New Member
    • Oct 2006
    • 4

    newbie question:why does this code work...

    hi all...
    i found this code in some past discussion on this forum.i do not understand why operator overloading is necessary.if i remove it iam getting errors...
    Code:
    #include <map>
    
    class Keys {
    public:
    Keys(int k1, int k2)  
    {
          key1=k1;
          key2=k2;
     }
    bool operator<(const Keys &right) const {
    return (key1 < right.key1 && key2 < right.key2);
    }
    int key1;
    int key2;
    };
    
    int main() {
    std::map<Keys, int> mymap;
    mymap.insert(std:air<Keys, int>(Keys(3, 8), 5));
    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are using a map of Keys objects. A map is a tree of ordered values. The map insert needs to know how to compare objects by calling the < operator to preserve the order.

    Comment

    • anagram
      New Member
      • Oct 2006
      • 4

      #3
      i get the gist of what you say.....
      but plzzzzz can u explain more about the ordering a lil bit.....
      it would be very helpful....

      Comment

      • anagram
        New Member
        • Oct 2006
        • 4

        #4
        thank you for the previous explanation...

        Comment

        Working...