15
2012
10

linux下练习 c++ 序列容器的使用

code:

// sequence.cpp 
/*
序列式容器:vector,deque,list
插入:.insert(position,n,element), .insert(position,pos_begin,pos_end)
赋值:.assign(n,element), .assign(pos_begin,pos_end)
调整:.resize(n,element=value)
首尾:.front(),.back()
增删:.push_back(element), .pop_back()----删除,返回void
*/
#include <iostream>
#include <deque>
#include <string>
#include "print.h"
using namespace std;
int main()
{
deque<string> ds;
ds.push_back("zhang");//增加
ds.push_back("pu");
ds.push_back("yang");
ds.push_back("xie");
ds.insert(ds.begin()+1,1,"wang");//插入
string s1[3]={"liao","hu","liu"};
ds.insert(----ds.end(),s1,s1+3);
print(ds.begin(),ds.end(),',');
ds.pop_back(); //删除最后一个
ds.pop_back();
print(ds.begin(),ds.end(),',');
ds.resize(10,"pkm");//大小设为10,后面用pkm填充
print(ds.begin(),ds.end(),',');
ds.assign(5,"kkkkk");//5个,都为kkkkk
print(ds.begin(),ds.end(),',');
return 0;
}


//print.h

//print.h
#include <iostream>
using namespace std;
#ifndef print_fun
#define print_fun
template<typename T>
///显示序列数据
void print(T b,T e,char c=' ')
{
bool isExit=false;
while (b!=e)
{
cout<<*b++<<c;
isExit=true;
}
if(isExit) cout<<endl;
}
#endif





版权声明:
作者:真爱无限 出处:http://www.pukuimin.top 本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.
« 上一篇下一篇 »

相关文章:

评论列表:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。