// #include<iostream> #include<vector> #include<string> using namespace std; int main() { vector<string> svec; svec.reserve(1024); string text_word; while(cin >> text_word) { svec.push_back(text_word); } vector<string>::iterator iter=svec.begin(); vector<string>::iterator iter_end=svec.end(); for(;iter!=iter_end;iter++) { cout<<(*iter)<<endl; } return 0; }
程序2:ios
#include<iostream> #include<vector> #include<string> using namespace std; void even_odd(const vector<int> ivec,vector<int> ivec_even,vector<int> ivec_odd) { vector<int>::const_iterator iter=ivec.begin(); vector<int>::const_iterator iter_end=ivec.end(); for(;iter!=iter_end;iter++) { if(*iter%2==0) ivec_even.push_back(*iter); else ivec_odd.push_back(*iter); } } int main() { vector<int> ivec; for(int i=0;i<20;i++) ivec.push_back(i); vector<int> ivec_even; vector<int> ivec_odd; even_odd(ivec,ivec_even,ivec_odd); cout<<"奇數容器內:"<<endl; vector<int>::iterator iter=ivec_even.begin(); vector<int>::iterator iter_end=ivec_even.end(); for(;iter!=iter_end;iter++) { cout<<*iter<<" "; } cout<<"偶數容器內:"<<endl; vector<int>::iterator iter1=ivec_odd.begin(); vector<int>::iterator iter_end1=ivec_odd.end(); for(;iter1!=iter_end1;iter1++) { cout<<*iter1<<" "; } return 0; }
程序3
spa
#include<iostream> #include<list> #include<string> using namespace std; int main() { int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 }; list<int> ilist(ia,ia+11); list<int>::iterator iter=ilist.begin(); list<int>::iterator iter_end=ilist.end(); bool state=false; for(;iter!=iter_end;) { if(state) ilist.erase(iter++); else iter++; state=!state; } iter=ilist.begin(); iter_end=ilist.end(); for(;iter!=iter_end;iter++) { cout<<*iter<<endl; } return 0; }