Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Sort Issue -- other ways to approach without changing the container type from list

0 votes
asked Dec 10, 2018 by Arivu Anukanth (220 points)

  list.sort(students.begin(), students.end(), sortByLastName); //std:: list.
        cout << "Sorted names: \n"  << endl;
        while (i != students.end())
        cout << *i << endl;
        ++i;
     }
}

 error: missing template arguments before ‘.’ token
        list.sort(students.begin(), students.end(), sortByLastName);

if I use Std::sort with 
#include <algorithm> it gives me a 
Compilation failed due to following error(s).
In file included from /usr/include/c++/5/algorithm:62:0,
                 from main.cpp:14:
/usr/include/c++/5/bits/stl_algo.h: In instantiation of ‘void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_List_iterator<DataType>; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<bool (*)(DataType, DataType)>]’:
/usr/include/c++/5/bits/stl_algo.h:4729:18:   required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = std::_List_iterator<DataType>; _Compare = bool (*)(DataType, DataType)]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',79)">main.cpp:79:67</span>:   required from here
/usr/include/c++/5/bits/stl_algo.h:1964:22: error: no match for ‘operator-’ (operand types are ‘std::_List_iterator’ and ‘std::_List_iterator’)
     std::__lg(__last - __first) * 2,
                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from main.cpp:10:
/usr/include/c++/5/bits/stl_iterator.h:333:5: note: candidate: template decltype ((__x.base() - __y.base())) std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator-(const reverse_iterator<_Iterator>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:333:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/algorithm:62:0,
                 from main.cpp:14:
/usr/include/c++/5/bits/stl_algo.h:1964:22: note:   ‘std::_List_iterator’ is not derived from ‘const std::reverse_iterator<_Iterator>’
     std::__lg(__last - __first) * 2,
                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from main.cpp:10:
/usr/include/c++/5/bits/stl_iterator.h:387:5: note: candidate: template decltype ((__y.base() - __x.base())) std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator-(const reverse_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:387:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/algorithm:62:0,
                 from main.cpp:14:
/usr/include/c++/5/bits/stl_algo.h:1964:22: note:   ‘std::_List_iterator’ is not derived from ‘const std::reverse_iterator<_Iterator>’
     std::__lg(__last - __first) * 2,
                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from main.cpp:10:
/usr/include/c++/5/bits/stl_iterator.h:1145:5: note: candidate: template decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
     operator-(const move_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:1145:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/algorithm:62:0,
                 from main.cpp:14:
/usr/include/c++/5/bits/stl_algo.h:1964:22: note:   ‘std::_List_iterator’ is not derived from ‘const std::move_iterator<_Iterator>’
     std::__lg(__last - __first) * 2,
                      ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from main.cpp:10:
/usr/include/c++/5/bits/stl_iterator.h:1152:5: note: candidate: template decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
     operator-(const move_iterator<_Iterator>& __x,
     ^
/usr/include/c++/5/bits/stl_iterator.h:1152:5: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/5/algorithm:62:0,
                 from main.cpp:14:
/usr/include/c++/5/bits/stl_algo.h:1964:22: note:   ‘std::_List_iterator’ is not derived from ‘const std::move_iterator<_Iterator>’
     std::__lg(__last - __first) * 2,

2 Answers

0 votes
answered Jan 16, 2019 by Ryan (1,230 points)
Your issue is that you are calling the sort() algorithm wrong. In the code below, it is written correctly.

#include <algorithm>
#include <list>

using namesapce std;

int main()
{
    list<int> myList = {1, 3, 5, 2, 10}; // Make a new list
    
    sort(myList.begin(), myList.end()); // Sort the list
}

If you wish to call the sort() member function for a list container, list.sort(), it is declared with either 1 or no parameters. You also wouldn't need to include the algorithm header file, as list.sort() is part of the list header.

Hope this helps!
0 votes
answered Feb 8, 2019 by layman806
You cannot use std::sort for std::list. That is because std::list doesn't allow random access, but std::sort requires random access iterator. If you really want to use std::sort, use std::vector instead of the std::list.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...