Sorting list usind STL

//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
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.
I change it, but now I have another error:
Error C2955 'std::list': use of class template requires template argument list
Last edited on
I change it, but now I have another error:

When you change something and it generates an error, it is customary to supply the changed code that produces the error so that nobody has to break out a crystal ball to figure out what you're talking about.
Here is code:
list(objects.begin(), objects.end());
cout << "List: ";
for (objectsIt= objects.begin(); objectsIt!= objects.end(); objectsIt++)
{
cout << *objectsIt<< " ";
}
cout << endl;
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.
Topic archived. No new replies allowed.