STL中序列式容器的共性
代码如下:
/* * vector_1.cpp * * Created on: 2013年8月6日 * Author: Administrator */ #include <iostream> #include <deque> #include <string> using namespace std; template <typename T> void print(T b , T e,char c = ' '){ while(b!= e){ cout<<*b++<<c; } cout<<endl; } int main(){ deque<string> ds; //push_back(element) .从后面增加一个元素 ds.push_back("liuyifei");//在末尾增加一个元素 ds.push_back("刘亦菲"); ds.push_back("liushishi"); ds.push_back("刘诗诗"); ds.push_back("zzt"); print(ds.begin(),ds.end(),','); //insert(pos,n,element).在pos这个位置上插入n个element ds.insert(++++ds.begin(),2,"章泽天"); print(ds.begin(),ds.end(),','); //insert(pos,begin,end).在pos位置上插入begin~end之间的数据 string s[3] = {"allen","hjd","黄东东"}; ds.insert(----ds.end(),s,s+3); print(ds.begin(),ds.end(),','); //pop_back().从末尾删除一个元素 ds.pop_back();ds.pop_back(); print(ds.begin(),ds.end(),','); //front().返回序列的第一个元素(返回的是引用) //end().返回序列的最后一个元素 cout<<"front:"<<ds.front()<<",back:"<<ds.back()<<endl; //resize(n,element).把序列的大小设为n,,不够的元素用element来补充 ds.resize(12,"小东东"); print(ds.begin(),ds.end(),','); //assign(n,element)。将序列的内容设置成n个element ds.assign(5,"hdd"); print(ds.begin(),ds.end()); ds.front() = "帅哥东"; ds.back() = "ddd"; print(ds.begin(),ds.end()); }
结果如下:
liuyifei,刘亦菲,liushishi,刘诗诗,zzt, liuyifei,刘亦菲,章泽天,章泽天,liushishi,刘诗诗,zzt, liuyifei,刘亦菲,章泽天,章泽天,liushishi,allen,hjd,黄东东,刘诗诗,zzt, liuyifei,刘亦菲,章泽天,章泽天,liushishi,allen,hjd,黄东东, front:liuyifei,back:黄东东 liuyifei,刘亦菲,章泽天,章泽天,liushishi,allen,hjd,黄东东,小东东,小东东,小东东,小东东, liuyifei,刘亦菲,章泽天,章泽天,liushishi, hdd hdd hdd hdd hdd 帅哥东 hdd hdd hdd ddd