473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sorting a std::list

I tried taking a list and pass it through std::sort like the following:
sort(Unsorted.begin(), Unsorted.end());
I got an error back stating that the list iterator doesn't have a
binary substraction operator.
Peeking in the algoritm header file it's clear why seeing that sort
there calls _sort(_First, _Last, _Last-_First) which might be a tad
challenging seeing that the different values stored in a list do not
need to be stored consecutively.
My question is there another way to sort the list in the STL?

Jul 22 '05 #1
11 5758
ve*********@hotmail.com wrote:
I tried taking a list and pass it through std::sort like the following:
sort(Unsorted.begin(), Unsorted.end());
I got an error back stating that the list iterator doesn't have a
binary substraction operator.
Peeking in the algoritm header file it's clear why seeing that sort
there calls _sort(_First, _Last, _Last-_First) which might be a tad
challenging seeing that the different values stored in a list do not
need to be stored consecutively.
My question is there another way to sort the list in the STL?

I think you want

list::sort( CompFunc op )

where

op( elem1, elem2 )

compares two elements?

( See 'The C++ Standard Library" - Josuttis p 245-6 )

After all, you have a list of somethings - the elem1's and elem2's - of
a type for which some way of ordering is presumably available. The
comparison function takes any two elements and determines their relative
ranking. The implementation may well complain if it has no way of telling.
Hope this helps.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #2
list::sort( CompFunc op )


You know, I feel really stupid now. I don't know how I managed to miss
the sort function of list.

Jul 22 '05 #3
Mike Hewson wrote:
I think you want

list::sort( CompFunc op )

where

op( elem1, elem2 )

compares two elements?

( See 'The C++ Standard Library" - Josuttis p 245-6 )

After all, you have a list of somethings - the elem1's and elem2's -
of a type for which some way of ordering is presumably available. The
comparison function takes any two elements and determines their relative
ranking. The implementation may well complain if it has no way of telling.
Hope this helps.


Also I found ( Josuttis again p 397 ) that std::sort and
std::stable_sort use *random access iterators*. Hence can't be used for
lists, as they are not provided for that type of container.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #4
ve*********@hotmail.com wrote:
list::sort( CompFunc op )

You know, I feel really stupid now. I don't know how I managed to miss
the sort function of list.

Tre Cool, dude.
Ah, I'm still *re-reading* Josuttis, two years later! :-)

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #5
ve*********@hotmail.com wrote:
I tried taking a list and pass it through std::sort like the following:
sort(Unsorted.begin(), Unsorted.end());
I got an error back stating that the list iterator doesn't have a
binary substraction operator.
Peeking in the algoritm header file it's clear why seeing that sort
there calls _sort(_First, _Last, _Last-_First) which might be a tad
challenging seeing that the different values stored in a list do not
need to be stored consecutively.
My question is there another way to sort the list in the STL?

someList.sort();
However I would expect an std::sort overload for std::list in an
implementation. Does anyone know if this is required by the standard?


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6
Ioannis Vranos wrote:
ve*********@hotmail.com wrote:
I tried taking a list and pass it through std::sort like the following:
sort(Unsorted.begin(), Unsorted.end());
I got an error back stating that the list iterator doesn't have a
binary substraction operator.
Peeking in the algoritm header file it's clear why seeing that sort
there calls _sort(_First, _Last, _Last-_First) which might be a tad
challenging seeing that the different values stored in a list do not
need to be stored consecutively.
My question is there another way to sort the list in the STL?


someList.sort();
However I would expect an std::sort overload for std::list in an
implementation. Does anyone know if this is required by the standard?


It is not (or at least, was not in 1998). The std::sort function in the
<algorithm> header only takes random access iterators. In any case, if
you're thinking you could write std::sort to take its two iterators and
delegate the sorting to a list object, I think you're mistaken. I don't
see how you would get access to the container object. (Cf. the situation
with the remove algorithm and the erase member function.)

--
Regards,
Buster
Jul 22 '05 #7
Buster wrote:
In any case, if
you're thinking you could write std::sort to take its two iterators and
delegate the sorting to a list object, I think you're mistaken. I don't
see how you would get access to the container object. (Cf. the situation
with the remove algorithm and the erase member function.)

Indeed, you are right about that. I think TC++PL states this somewhere.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #8
ve*********@hotmail.com wrote:
I tried taking a list and pass it through std::sort like the following:
sort(Unsorted.begin(), Unsorted.end());
I got an error back stating that the list iterator doesn't have a
binary substraction operator.
Peeking in the algoritm header file it's clear why seeing that sort
there calls _sort(_First, _Last, _Last-_First) which might be a tad
challenging seeing that the different values stored in a list do not
need to be stored consecutively.
My question is there another way to sort the list in the STL?

For a very good STL reference (especially if your STL implementation is
SGI's, which is fairly common I think (its free)), look at
http://www.sgi.com/tech/stl/

There, we see (http://www.sgi.com/tech/stl/sort.html):

template <class RandomAccessIterator>
void sort(RandomAccessIterator first, RandomAccessIterator last);

template <class RandomAccessIterator, class StrictWeakOrdering>
void sort(RandomAccessIterator first, RandomAccessIterator last,
StrictWeakOrdering comp);

Complexity
O(N log(N)) comparisons (both average and worst-case), where N is last -
first. [2]
So the sort function only takes Random Access Iterators, whereas
list::iterator is a Bidirectional Iterator.

Thus, we go to the list page (http://www.sgi.com/tech/stl/List.html),
and we have:

New members
These members are not defined in the Reversible Container, Front
Insertion Sequence, and Back Insertion Sequence requirements, but are
specific to list.
----
void sort(); Sorts *this according to operator<. The sort is stable,
that is, the relative order of equivalent elements is preserved. All
iterators remain valid and continue to point to the same elements. [6]
The number of comparisons is approximately N log N, where N is the
list's size.
----
[6] The sort algorithm works only for random access iterators. In
principle, however, it would be possible to write a sort algorithm that
also accepted bidirectional iterators. Even if there were such a version
of sort, it would still be useful for list to have a sort member
function. That is, sort is provided as a member function not only for
the sake of efficiency, but also because of the property that it
preserves the values that list iterators point to.

So, use the list::sort() function to sort a list, or if you need a
comparison function other than STL's less functor, there is a member
template version of list::sort(BinaryPredicate Comp) which lets you do that.

One warning: if you are not using SGI's STL, then watch out for SGI
extensions to the standard on this reference site.
Jul 22 '05 #9
Ioannis Vranos wrote:
someList.sort();

However I would expect an std::sort overload for std::list in an
implementation. Does anyone know if this is required by the standard?


It's not required. Depending on how the list is implemented, it may not
be possible to do it well. list::sort can work simply by rearranging
its pointers without copying. std::sort would not have access to the
list object itself, and would have trouble reassigning the 'begin' and
'end' data. It could work by swapping the values themselves, but it
would be no substitute for list::sort.

--
Dave O'Hearn

Jul 22 '05 #10
<ve*********@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
list::sort( CompFunc op )


You know, I feel really stupid now. I don't know how I managed to miss
the sort function of list.


Perhaps the approach I use might help:
When looking for a function to operate on
a container, I first peruse its member functions,
then if not found, look 'globally', e.g. the
stuff in <algorithm>. An aside, not specific to
your problem: Most of my books advise that if both
member and nonmember solutions are available, one
should generally prefer the member solution, as it's
more likely to have better performance. But of course,
depending upon the implementation used, YMMV.

-Mike
Jul 22 '05 #11
Ioannis Vranos wrote:
ve*********@hotmail.com wrote:
I tried taking a list and pass it through std::sort like the following:
sort(Unsorted.begin(), Unsorted.end());
I got an error back stating that the list iterator doesn't have a
binary substraction operator.
Peeking in the algoritm header file it's clear why seeing that sort
there calls _sort(_First, _Last, _Last-_First) which might be a tad
challenging seeing that the different values stored in a list do not
need to be stored consecutively.
My question is there another way to sort the list in the STL?


someList.sort();
However I would expect an std::sort overload for std::list in an
implementation. Does anyone know if this is required by the standard?


C++ 2003 standard : Section 23.2.2.4 - mentions about the sort()
method in the list container, with a complexity of (n.log n).

--
Karthik.
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
13
by: Nenad Jalsovec | last post by:
using std::list; struct Something{ Something( val ): value( val ){} bool operator<( Something & s ){ return value < s.value; } int value; }; main(){ list< Something * > things;
5
by: matthias_k | last post by:
Hi, I need to sort elements of a std::list using a function predicate, something like: bool predicate( const& M m1, const& M m2 ) { return m1.somedata < m2.somedata; } I tried to call...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.