Apr 30, 2016 at 6:09pm UTC
//In subject should be Sorting list using STL.
I have list and I want to sort them in the ascending order
list <Object*> objects;
list<Object*>::iterator objectsIt;
Here is the code I wrote:
sort(objects.begin(), objects.end());
cout << "List: ";
for (objectsIt= objects.begin(); objectsIt!= objects.end(); objectsIt++)
{
cout << *objectsIt<< " ";
}
cout << endl;
And I have many errors in file algorithm e. g.
Error C2784
'unknown-type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)': could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::_List_iterator<std::_List_val<std::_List_simple_types<Object*>>>' alit
Could you help me?
Last edited on Apr 30, 2016 at 6:12pm UTC
Apr 30, 2016 at 6:34pm UTC
std::sort requires random access iterators. std::list is not a random access container, so you can't use std::sort to sort it.
Instead, use the sort method of std::list .
Apr 30, 2016 at 6:48pm UTC
I change it, but now I have another error:
Error C2955 'std::list': use of class template requires template argument list
Last edited on Apr 30, 2016 at 6:54pm UTC
Apr 30, 2016 at 6:54pm UTC
Here is code:
list(objects.begin(), objects.end());
cout << "List: ";
for (objectsIt= objects.begin(); objectsIt!= objects.end(); objectsIt++)
{
cout << *objectsIt<< " ";
}
cout << endl;
Apr 30, 2016 at 7:02pm UTC
On the first line you are attempting to construct a temporary list from a sequence.
It's as simple as objects.sort();
to use the default sorting criteria.