#include <iostream>#include <vector>#include <list>#include <deque>#include <set>#include <algorithm>using namespace std;int main(){list<int> coll1;for(int i=1;i<=9;++i) {coll1.push_back(i);}vector<int> coll2;copy(coll1.begin(),coll1.end(),back_inserter(coll2));deque<int> coll3;copy(coll1.begin(),coll1.end(),front_inserter(coll3));set<int> coll4;copy(coll1.begin(),coll1.end(),inserter(coll4,coll4.begin()));for(i=0;i<=9;++i)cout<<*coll2[i]<<endl;for(i=0;i<=9;++i)cout<<*coll3[i]<<endl;for(i=0;i<=9;++i)cout<<*coll4[i]<<endl;}不知道哪位大哥能不能给我解决一下,谢谢拉
參考答案:#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll1;
for(int i=1;i<=9;++i) {
coll1.push_back(i);
}
vector<int> coll2;
copy(coll1.begin(),coll1.end(),back_inserter(coll2));
deque<int> coll3;
copy(coll1.begin(),coll1.end(),front_inserter(coll3));
set<int> coll4;
copy(coll1.begin(),coll1.end(),inserter(coll4,coll4.begin()));
for(i=0;i<=8;++i)
cout<<coll2[i]<<endl;
for(i=0;i<=8;++i)
cout<<coll3[i]<<endl;
copy(coll4.begin(),coll4.end(),ostream_iterator<int>(cout,"\n"));
}
这样就可以了,STL的应用呵。