Topic: Map and comparators
Author: whbloodworth@usa.net (William H. Bloodworth)
Date: 1999/11/09 Raw View
On 8 Nov 1999 16:52:58 GMT, Darin Adler <darin@bentspoon.com> wrote:
An extreme error on my part. I originally did not make 'operator <' a const method.
That caused compilation errors which I attributed to other problems.
Thank you all for you assistance.
William B.
>> Suppose I have a class that I wish to use as the key in a map called Foo.
>>
>> The STL map seems to "force" the class implementor to create a non-intuitive
>> comparator (Foo::FooComparator) instead of using the defined operator <.
>
>Not true. If you define a map like this
>
> std::map<Foo, int> m1;
>
>the map will use < on the Foo keys. You have the *option* of using a
>comparator other than < like this
>
> std::map<Foo, int, FooCompareFunctor> m2;
>
>but it's not required.
==========================================================================
= Great Achievement REQUIRES Great Effort.
=
= William H. Bloodworth - whbloodworth@usa."net" - Maxim Group Consultant
==========================================================================
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: whbloodworth@usa.net (William H. Bloodworth)
Date: 1999/11/07 Raw View
Suppose I have a class that I wish to use as the key in a map called Foo.
The STL map seems to "force" the class implementor to create a non-intuitive
comparator (Foo::FooComparator) instead of using the defined operator <. This, to
me, is a major pain and seems to be contrary to the simple C++ paradigm of just
defining operator <.
Is there a way to tell the map to use operator < as the comparator?
class Foo
{
public:
Foo (int newValue) : Data(newValue) { }
int GetData (void) const { return Data; }
// Required by the STL map to compare Foo objects.
class FooComparator
{
public:
bool operator () (const Foo & lhs, const Foo & rhs) const
{
return lhs.GetData() < rhs.GetData();
}
};
// This operator is NOT used by the STL map to compare Foo objects.
bool operator < (const Foo & rhs) const { return GetData() < rhs.GetData(); }
private:
int Data;
};
Wil Bloodworth
==========================================================================
= Great Achievement REQUIRES Great Effort.
=
= William H. Bloodworth - whbloodworth@usa."net" - Maxim Group Consultant
==========================================================================
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/08 Raw View
"William H. Bloodworth" wrote:
>
> Suppose I have a class that I wish to use as the key in a map called Foo.
>
> The STL map seems to "force" the class implementor to create a non-intuitive
> comparator (Foo::FooComparator) instead of using the defined operator <. This, to
> me, is a major pain and seems to be contrary to the simple C++ paradigm of just
> defining operator <.
>
> Is there a way to tell the map to use operator < as the comparator?
Yes - don't provide a comparator. The comparator defaults to
std::less<Key>, which uses '<', unless it's been specialized for Key to
do something different.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/11/08 Raw View
On 7 Nov 1999 18:47:57 GMT, whbloodworth@usa.net (William H. Bloodworth)
wrote:
:
: Suppose I have a class that I wish to use as the key in a map called Foo.
[snip]
: Is there a way to tell the map to use operator < as the comparator?
map<Foo, X> m;
Your compiler may not support default template parameters. In that
case, use
map<Foo, X, less<Foo> > m;
John
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Darin Adler <darin@bentspoon.com>
Date: 1999/11/08 Raw View
William H. Bloodworth <whbloodworth@usa.net> wrote:
> Suppose I have a class that I wish to use as the key in a map called Foo.
>
> The STL map seems to "force" the class implementor to create a non-intuitive
> comparator (Foo::FooComparator) instead of using the defined operator <.
Not true. If you define a map like this
std::map<Foo, int> m1;
the map will use < on the Foo keys. You have the *option* of using a
comparator other than < like this
std::map<Foo, int, FooCompareFunctor> m2;
but it's not required.
-- Darin
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jeff Greif <jmg@trivida.com>
Date: 1999/11/08 Raw View
The default comparator for std::map<Key,Value> is
struct std::less<Key> : public std::binary_function<Key,Key,bool> {
bool operator()(const Key& lhs, const Key& rhs) const {
return lhs < rhs;
}
};
which appears to do exactly what you want.
Jeff
"William H. Bloodworth" wrote:
> Suppose I have a class that I wish to use as the key in a map called Foo.
>
> The STL map seems to "force" the class implementor to create a non-intuitive
> comparator (Foo::FooComparator) instead of using the defined operator <. This, to
> me, is a major pain and seems to be contrary to the simple C++ paradigm of just
> defining operator <.
>
> Is there a way to tell the map to use operator < as the comparator?
>
....
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: scherrey@switchco.com (Benjamin Scherrey)
Date: 1999/11/08 Raw View
I'm using g++ 2.95.1 and it automatically uses my defined less than operator
for the key type. How are you defining your map? I'm using the default
comparitor (which is less< key_type >) and also one with a specified
comparitor, greater< key_type >. Both are automatically using my operator<
defined for the class. Is g++ wrong or do you have a bad STL implementation?
regards & later,
Ben Scherrey
In article <38259b5d.474995487@news.airmail.net>, whbloodworth@usa.net wrote:
>Suppose I have a class that I wish to use as the key in a map called Foo.
>
>The STL map seems to "force" the class implementor to create a non-intuitive
>comparator (Foo::FooComparator) instead of using the defined operator <. This,
> to
>me, is a major pain and seems to be contrary to the simple C++ paradigm of just
>defining operator <.
>
>Is there a way to tell the map to use operator < as the comparator?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]