C++中迭代器做形参,
//下面是代码:问题在注释里面。我是个初学者,望大家不吝赐教
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
template<class T>
void output(list<T>& coll,list<int>::iterator& pos)//当我把此处int改成T后,就编译错误,为什么此处不支持模板呢?
{
for(pos=coll.begin();pos!=coll.end();++pos)
{
cout<<*pos<<" ";
}
}
int main()
{
list<int> coll1;
list<int>::iterator pos;
coll1.push_back(30);
coll1.push_back(0);
coll1.push_back(2);
coll1.push_back(9);
coll1.push_back(2);
coll1.push_back(1);
coll1.push_back(1);
coll1.push_back(87);
output(coll1,pos);
return 0;
}
[解决办法]
typename list<T>::iterator