1

I would like to sort my list with the Standard algorithm std::sort.

Here is my try:

#include <list>
#include <algorithm>

template<typename T>
class MyList {
private:
    std::list<T> myList;        
public:
    void add(T item) {
        myList.push_back(item);
    }

    void mySort() {
        std::sort(myList.begin(), myList.end());
    }
};

Compile error:

    c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5475:22: note: candidates are:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:67:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\char_traits.h:39,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:40,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from ..\src\firstone.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:327:5: note: template<class _Iterator> typename std::reverse_iterator<_Iterator>::difference_type std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator-(const reverse_iterator<_Iterator>& __x,
     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:327:5: note:   template argument deduction/substitution failed:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\algorithm:62:0,
                 from ..\src\mylist.h:5,
                 from ..\src\firstone.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5475:22: note:   'std::_List_iterator<int>' is not derived from 'const std::reverse_iterator<_Iterator>'
     std::__lg(__last - __first) * 2);
                      ^
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:67:0,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\char_traits.h:39,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ios:40,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\iostream:39,
                 from ..\src\firstone.cpp:1:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:384:5: note: template<class _IteratorL, class _IteratorR> typename std::reverse_iterator<_Iterator>::difference_type std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator-(const reverse_iterator<_IteratorL>& __x,
     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_iterator.h:384:5: note:   template argument deduction/substitution failed:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\algorithm:62:0,
                 from ..\src\mylist.h:5,
                 from ..\src\firstone.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algo.h:5475:22: note:   'std::_List_iterator<int>' is not derived from 'const std::reverse_iterator<_Iterator>'
     std::__lg(__last - __first) * 2);
                      ^

18:57:32 Build Finished (took 986ms)

What am I doing wrong?

1
  • You may use std::vector instead of std::list. Commented Jan 1, 2014 at 18:09

5 Answers 5

6

You're trying to sort a list. Lists don't support random access iterators which are required for the std::sort to work. (Lists support only bidirectional iterators.)

In my humble opinion, this is faulty logic coming from the "great gods of C++".

You can use the list::sort member function to sort a list.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, the great gods of C++ provide std::list::sort. I guess std::sort could have been specialized to use that.
@juanchopanza doesn't matter when writing generic code, I would need to provide a silly specialization for lists (and forward lists for that matter) just because of a silly implementation detail that for some reason made its way to semantics of sorting
@juanchopanza container concept (or something like sortable which is actually slightly fuzzy) should surely not require a random access iterator
1

The standard algorithm std::sort requires random access iterators,std::list<>::iterators are bidirectional iterators)

Use member function std::list<>::sort to sort the list

Comments

1

You basically have two choices: either switch to using std::list's sort() member function:

myList.sort();

...or else switch to a different container type such as vector or deque, that supports std::sort by providing random access iterators.

In nearly every possible case, the latter is the correct choice; std::list is (in my opinion) nearly always a mistake.

Comments

1

std::list does not have random access iterators, which is a requirement for std::sort. But you can use the std::list::sort member function instead.

Note that this assumes that you need an std::list in the first place. In my experience this is rarely the case in real code. It could well be that an std::vector would suffice.

Comments

0

Use code below:

  void mySort() 
  {
    myList.sort();
    //std::sort(myList.begin(), myList.end());
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.