C++顺序容器操作

//con_insert.cc
#include <iostream> #include <list> #include <string> #include <vector> #include <iterator> using namespace std; int main(int argc,char *argv[]){ list<string> strlist; string str; { while(cin >> str && str!=".") strlist.insert(strlist.begin(),str); } //vector<int>::iterator it; for (list<string>::iterator it = strlist.begin(); it != strlist.end(); ++it){ cout << *it; } cout << endl; //cout << strlist.begin()<< endl; return 0; }


//con__insert.cc
#include <iostream> #include <iterator> #include <vector> #include <string> #include <list> using namespace std; int main() { list<string> lst; string word = ""; list<string>::iterator it = lst.begin(); //list<string>::iterator iter(it); while(cin >> word && word !="."){ it = lst.insert(it,word); //cout << *it; } cout << endl; list<string>::iterator iter = lst.begin(); while(iter != lst.end()){ cout << *iter++; } cout << endl; //cin >> word; //cout << *lst.insert(it,word) << endl; list<string> llst(3,"hi"); for(iter = llst.begin();iter!=llst.end();iter++){ cout << *iter; } cout << endl; return 0; }

//con_insert_more.cc
#include <iostream> #include <list> #include <string> #include <vector> #include <iterator> using namespace std; int main(int argc,char *argv[]){ list<string> strlist; string str; { while(cin >> str && str!=".") strlist.insert(strlist.end(),10,str); } //vector<int>::iterator it; for (list<string>::iterator it = strlist.begin(); it != strlist.end(); ++it){ cout << *it; } cout << endl; //cout << strlist.begin()<< endl; return 0; }
PS:运行结果

posted @ 2011-06-19 23:53  Podevor  阅读(154)  评论(0编辑  收藏  举报