我目前正在嘗試爲列表編寫一個循環。我的代碼是:如何爲列表編寫for循環?
template<typename T>
void Bubblesorting(list<T> & mylist)
{
typename T::const_iterator it1;
typename T::const_iterator it2;
for(it1=mylist.begin();it1!=mylist.end();it1++)
for(it2=mylist.begin();it2!=mylist.end()-(it1-begin());it2++)
if((*(std::next(it2,1))<*it2)
swap((*(std::next(it2,1)),*it2);
cout << *it2 << ' ';
}
編譯失敗:
error C2958: the left parenthesis '(' was not matched correctly
能否請你幫我檢測究竟哪裏出了問題我怎麼能寫一個for循環列表的元素?
列表迭代器不是隨機訪問,因此'+'運算符不起作用,並且您正在傳遞對const容器的引用,同時試圖在方法內對其進行變異。 – bobah