Topic: std::list (sort)


Author: "P.J. Plauger" <pjp@dinkumware.com>
Date: Mon, 24 Sep 2001 16:49:14 GMT
Raw View
"James Russell Kuyper Jr." <kuyper@wizard.net> wrote in message news:3BAB21C1.7DED340@wizard.net...

> > Thanks. I still don't understand the reference to greater<> in the
> > documentation. I would expect this to be some form of less.
>
> According to the standard, the form of std::list<T>::sort() for which
> the caller does not provide a Compare object uses operator<(). It uses
> neither greater<>, nor less<>. If an implementation does use greater<>,
> it's non-conforming.

Our list<T>::sort does indeed use operator<, as it should. For an
implementation that does not support member template functions, we cannot
conform exactly anyway, so we try to do something useful. For example,
we supply one or more ordinary member functions as a replacement, with
common or likely hard-wired parameter types. We chose greater<T> as the
functor type for list sort and merge to supply a modicum of variety.

Now that VC++ has adequate support for member template functions, the
issue is largely mooted. Some are easy to add to existing VC++ headers,
as Pete Becker described. You can license our upgrade library for VC++
and get essentially all of the required member template functions. And
one fine day V7.0 will ship with all these member template functions in
the library.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Martijn Lievaart <xnews.public.home@bogus.rtij.nl>
Date: Thu, 20 Sep 2001 16:50:53 GMT
Raw View
[ I took the liberty of adding comp.std.c++ to the newsgroups, as my answer
turns into a question on standards complience ]

sinny <sinny@telebot.com> dared to utter in
news:1oihqt0m7827lbie852coe04tjgipm23hv@4ax.com:

> if I have a struct, say
>
> struct some_struct
> {
>      int     a, b, c;
> };
>
> And then I create a list of that struct. How do i then sort the list
> using the STL sort algorithm so that the sort uses the struct member,
> b, as the sorting criterion?
>

First, don't use std::sort on lists, use std::list::sort. std::sort needs
random access iterators, which list cannot provide, but list luckily has
its own sort.

Now on with the show! Sorting on something else than operator< is luckily
very easy. You have to create a predicate. This can be a function or a so
called functor, an object that has operator() defined. For this the
function approach is easiest.

bool sort_on_b(const some_struct &lhs, const some_struct &rhs)
{
     return lhs.b < rhs.b;
}

list<some_struct> l;

// fill list

l.sort(sort_on_b);

Whow, wait a minute. This does not compile with the standard STL from VC6!
It does compile with VC6 with STLPort.

Looking further, I see two implementations. Dinkumware (who supplied the
MSVC STL) and the rest of the world, as far as I checked. Intuitively I say
the Dinkumware version is wrong. It's different from the rest of the STL,
and that is something that the STL normally doesn't do with good reason. As
STLPort does compile the above, I think they have it correct.

Anyway, to use Dinkumware, use something like:

namespace std
{
     // This is one instance where you have to re-open std.

 template<> struct greater<some_struct> : public
binary_function<some_struct, some_struct, bool>
 {
     bool operator()(const some_struct& x, const some_struct& y) const
  {
      return x.b > y.b;
  }
 };
}

 l.sort(greater<some_struct>());

I did compile the code, but I didn't test it. Try it out and let us know
what happens. BTW, I can advice to have a look at www.STLPort.org for a
very good STL implementation.

Any guru out there who can shed some more light on this? Who's right? What
does the Dinkumware implementation of using std::greater<> mean?

HTH,
M4
--
alt.comp.lang.learn.c-c++ FAQ:
http://snurse-l.org/acllc-c++/faq.html

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Pete Becker <petebecker@acm.org>
Date: Thu, 20 Sep 2001 18:12:11 GMT
Raw View
Martijn Lievaart wrote:
>
> Looking further, I see two implementations. Dinkumware (who supplied the
> MSVC STL) and the rest of the world, as far as I checked. Intuitively I say
> the Dinkumware version is wrong. It's different from the rest of the STL,
> and that is something that the STL normally doesn't do with good reason. As
> STLPort does compile the above, I think they have it correct.

The library code that ships with the Microsoft compiler was written for
VC++ 5.0, which had rather spotty support for member templates. For
newer versions of the compiler, edit the header <list> and change

void sort(_Pr3 _Pred)

to

template <class _Pr3>
void sort(_Pr3 _Pred)

which is the code that comes with current versions of our library.

--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Martijn Lievaart <xnews.public.home@bogus.rtij.nl>
Date: Fri, 21 Sep 2001 03:49:21 CST
Raw View
Pete Becker <petebecker@acm.org> dared to utter in
news:3BAA2EBF.A3165258@acm.org:

> Martijn Lievaart wrote:
>>
>> Looking further, I see two implementations. Dinkumware (who supplied
>> the MSVC STL) and the rest of the world, as far as I checked.
>> Intuitively I say the Dinkumware version is wrong. It's different from
>> the rest of the STL, and that is something that the STL normally
>> doesn't do with good reason. As STLPort does compile the above, I
>> think they have it correct.
>
> The library code that ships with the Microsoft compiler was written for
> VC++ 5.0, which had rather spotty support for member templates. For
> newer versions of the compiler, edit the header <list> and change
>
> void sort(_Pr3 _Pred)
>
> to
>
> template <class _Pr3>
> void sort(_Pr3 _Pred)
>
> which is the code that comes with current versions of our library.
>

Thanks. I still don't understand the reference to greater<> in the
documentation. I would expect this to be some form of less.

M4
--
alt.comp.lang.learn.c-c++ FAQ:
http://snurse-l.org/acllc-c++/faq.html

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: Fri, 21 Sep 2001 22:46:53 GMT
Raw View
Martijn Lievaart wrote:
>
> Pete Becker <petebecker@acm.org> dared to utter in
> news:3BAA2EBF.A3165258@acm.org:
>
> > Martijn Lievaart wrote:
> >>
> >> Looking further, I see two implementations. Dinkumware (who supplied
> >> the MSVC STL) and the rest of the world, as far as I checked.
> >> Intuitively I say the Dinkumware version is wrong. It's different from
> >> the rest of the STL, and that is something that the STL normally
> >> doesn't do with good reason. As STLPort does compile the above, I
> >> think they have it correct.
> >
> > The library code that ships with the Microsoft compiler was written for
> > VC++ 5.0, which had rather spotty support for member templates. For
> > newer versions of the compiler, edit the header <list> and change
> >
> > void sort(_Pr3 _Pred)
> >
> > to
> >
> > template <class _Pr3>
> > void sort(_Pr3 _Pred)
> >
> > which is the code that comes with current versions of our library.
> >
>
> Thanks. I still don't understand the reference to greater<> in the
> documentation. I would expect this to be some form of less.

According to the standard, the form of std::list<T>::sort() for which
the caller does not provide a Compare object uses operator<(). It uses
neither greater<>, nor less<>. If an implementation does use greater<>,
it's non-conforming.

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]