真爱无限的知识驿站

学习积累技术经验,提升自身能力

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



发表评论:

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

Powered By Z-BlogPHP 1.7.3

Copyright 2024-2027 pukuimin Rights Reserved.
粤ICP备17100155号