map comparison function for a user defined key

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eastern_strider

    map comparison function for a user defined key

    I'm running into problems about defining a comparison function for a
    map which has a user defined key. For example:

    class Key {
    public:
    string name;
    int number;

    Key (na, nu) : name (na), number (nu) {}
    bool operator< (const Key &key) const; //my question is how to
    define this function
    };

    How can I most efficiently define this comparison function? I have a
    workaround, which I suspect is not the right way:

    bool Key::operator< (const Key &key) const {
    stringstream stream1;
    stringstream stream2:
    stream1 << name << number;
    stream2 << k.name << k.number;
    return stream1.str() < stream2.str();
    }

    I believe this is not the right way, or is it? I tried other things
    such as:

    bool Key::operator< (const Key &key) const {
    return number < key.number;
    }

    but then in this case I defeat the whole purpose of defining a key
    based on a unique combination of a string, integer pair.

    Thanks for any help.

  • mlimber

    #2
    Re: map comparison function for a user defined key

    eastern_strider wrote:[color=blue]
    > I'm running into problems about defining a comparison function for a
    > map which has a user defined key. For example:
    >
    > class Key {
    > public:
    > string name;
    > int number;
    >
    > Key (na, nu) : name (na), number (nu) {}
    > bool operator< (const Key &key) const; //my question is how to
    > define this function
    > };
    >
    > How can I most efficiently define this comparison function? I have a
    > workaround, which I suspect is not the right way:
    >
    > bool Key::operator< (const Key &key) const {
    > stringstream stream1;
    > stringstream stream2:
    > stream1 << name << number;
    > stream2 << k.name << k.number;
    > return stream1.str() < stream2.str();
    > }
    >
    > I believe this is not the right way, or is it? I tried other things
    > such as:
    >
    > bool Key::operator< (const Key &key) const {
    > return number < key.number;
    > }
    >
    > but then in this case I defeat the whole purpose of defining a key
    > based on a unique combination of a string, integer pair.
    >
    > Thanks for any help.[/color]

    First, you should probably make your comparison a non-member function
    in the same namespace as Key. That will allow implicit conversions on
    the left-hand argument. See _C++ Coding Standards_, Items 27 and 44.
    Ordinarily, you might also make it a friend function, but because of
    the public data and lack of member functions, it looks like your Key
    class is really just an aggregation, rather than an abstraction, and so
    friendship is unnecessary.

    Now, to answer your question: You can make the less-than operator
    behave however is appropriate for your class. It looks like what you
    want might be something like this:

    bool Key::operator< ( const Key& k1, const Key& k2 )
    {
    return (k1.name < k2.name) && (k1.number < k2.number);
    }

    Cheers! --M

    Comment

    • mlimber

      #3
      Re: map comparison function for a user defined key


      mlimber wrote:[color=blue]
      > eastern_strider wrote:[color=green]
      > > I'm running into problems about defining a comparison function for a
      > > map which has a user defined key. For example:
      > >
      > > class Key {
      > > public:
      > > string name;
      > > int number;
      > >
      > > Key (na, nu) : name (na), number (nu) {}
      > > bool operator< (const Key &key) const; //my question is how to
      > > define this function
      > > };
      > >
      > > How can I most efficiently define this comparison function? I have a
      > > workaround, which I suspect is not the right way:
      > >
      > > bool Key::operator< (const Key &key) const {
      > > stringstream stream1;
      > > stringstream stream2:
      > > stream1 << name << number;
      > > stream2 << k.name << k.number;
      > > return stream1.str() < stream2.str();
      > > }
      > >
      > > I believe this is not the right way, or is it? I tried other things
      > > such as:
      > >
      > > bool Key::operator< (const Key &key) const {
      > > return number < key.number;
      > > }
      > >
      > > but then in this case I defeat the whole purpose of defining a key
      > > based on a unique combination of a string, integer pair.
      > >
      > > Thanks for any help.[/color]
      >
      > First, you should probably make your comparison a non-member function
      > in the same namespace as Key. That will allow implicit conversions on
      > the left-hand argument. See _C++ Coding Standards_, Items 27 and 44.
      > Ordinarily, you might also make it a friend function, but because of
      > the public data and lack of member functions, it looks like your Key
      > class is really just an aggregation, rather than an abstraction, and so
      > friendship is unnecessary.
      >
      > Now, to answer your question: You can make the less-than operator
      > behave however is appropriate for your class. It looks like what you
      > want might be something like this:
      >
      > bool Key::operator< ( const Key& k1, const Key& k2 )
      > {
      > return (k1.name < k2.name) && (k1.number < k2.number);
      > }[/color]

      Oops. Drop the member function qualifier (Key::) above.

      Cheers! --M

      Comment

      Working...