【发布时间】:2014-02-15 08:02:05
【问题描述】:
我目前正在尝试为 list 编写排序函数。我的代码是:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <list>
#include <string>
using namespace std;
template<typename T>
void sort(T &mylist)
{
typename std::list<T>::iterator it1;
typename std::list<T>::iterator it2;
for(it1=mylist.begin();it1!=mylist.end();it1++)
for(it2=mylist.begin();it2!=mylist.end()-it1;it2++)
if((*((it2)+1))<*it2)
swap((*((it2)+1)),*it2);
cout << *it1 << ' ';
}
int main()
{
list<int> List;
List.push_back(1);
List.push_back(12);
sort(List);
return 0;
}
编译失败:
error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::_List_iterator<_Mylist>' (or there is no acceptable conversion)
error C2784: ''unknown-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>'
error C2676: binary '-' : 'std::_List_iterator<_Mylist>' does not define this operator or a conversion to a type acceptable to the predefined operator
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::list<_Ty>' (or there is no acceptable conversion)
您能帮我找出问题所在吗?
【问题讨论】:
-
sort(List),而不是sort(&List)。你混淆了指针和迭代器。