Possible Duplicate:
Sort list using stl sort function
My question is can we sort two std::lists using std::sort function? I have 2 string lists
std::list<std::string>list1, list2;
.....//entering values to list
std::sort(list1.begin(), list1.end());
std::sort(list2.begin(), list2.end());
while i am sorting these lists i am getting error.
I tried with std::vector, at this time the sort works.
The error is like
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xutility(1158) :
see declaration of ‘std::operator -‘
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(3642):
error C2784: ‘_Base1::difference_type std::operator –
(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)’ :
could not deduce template argument for ‘const std::_Revranit<_RanIt,_Base> &’
from ‘std::_List_iterator<_Mylist>’
1> with
1> [
1> _Mylist=std::_List_valstd::string,std::allocator<std::string>
1> ]
I have to know that only std::sort supports lists?
You can’t use
std::sortto sortstd::list, becausestd::sortrequires iterators to be random access, andstd::listiterators are only bidirectional.However,
std::listhas a member functionsortthat will sort it: