C++ STL学习笔记三 deque双端队列容器

/*
 *
 ********************************************
 *  deque双端队列容器的基础说明:
 ********************************************
 * 
 *
 * 可进行随机访问,在**头部和尾端**插入、删除元素,时间复杂度为O(1)
 * Random Access Container   Back Insertion Sequence   Front Insertion Sequence
 * 
 ************************************************************************************ 
 * 当考虑容器元素的内存分配策略和操作的性能时,deque较vector更具优势    *
 * map管理块,块中包含一组数据,内存分配更细致(以块为单位,使用二级的MAP进行管理) *
 ************************************************************************************ 
 * 
 * 使用deque必须使用宏语句#include <deque>
 *
 **************************************************************************************
 *
 * 创建deque对象:
 * 1.deque<int> a;
 * 2.deque<int> a(10);      //具有10个元素的对象a,每个元素默认值为0
 * 3.deque<char> a(5,'k');
 * 4.deque<char> b(a);      //deque<char> c(a.begin(),a.end())
 *
 **************************************************************************************
 *
 * 初始化赋值
 * void push_back(const T& value)
 *
 **************************************************************************************
 *
 * 遍历访问
 * reference operator[](size_type n)
 * iterator begin()
 * iterator end()
 *
 **************************************************************************************
 *
 * 常用函数
 *
 * bool empty();
 * size_type size();size_type max_size();  //无size_type capacity();reverse();
 * reference front();reference back();
 * void pop_front();void push_front();   //相较vector增加部分
 * void pop_back();void push_back();
 * void clear();
 *            //还有些函数直接见代码如erase();
 *
 *
 *
 ********************************************
 * Author: cumirror   
 * Email: tongjinooo@163.com
 ********************************************
 *
 */

#include <iostream>
#include <deque>

 

int main()
{
 using namespace std;
 deque<int> a(10,5);


// 数组方式
 cout<<"数组方式访问:"<<endl;
 a[0]=100;
 for(int s=0;s<a.size();s++){
  cout<<a[s]<<endl;
 }


// 迭代器方式
 cout<<"迭代器方式访问:"<<endl;
 deque<int>::iterator i,iend;
 i=a.begin();
 iend=a.end();
 *i=101;
 for(deque<int>::iterator j=i;j!=iend;j++){
  cout<<*j<<endl;
 }


//注意插入删除时迭代器的失效问题,偷个懒,大家可以看下面的网页,里面有说明但没介绍原因,其实这与各个容器是如何实现的有很大关系,若想深究可看《STL源码剖析》
//http://blog.csdn.net/jokenchang2000/archive/2008/07/01/2603485.aspx
 cout<<"插入"<<endl;
 a.insert(a.begin(),102);


// 删除时注意,它是一个半闭区间
// 也支持 erase(iterator pos)单个元素的删除
 cout<<"删除"<<endl;
 a.erase(a.begin()+4,a.begin()+6);
 for(deque<int>::iterator k=a.begin();k!=a.end();k++){
  cout<<*k<<endl;
 }


// 头部插入
 a.push_front(999);


// 反向遍历访问
 cout<<"反向访问"<<endl;
 deque<int>::reverse_iterator ri;
 for(ri=a.rbegin();ri!=a.rend();ri++){
  cout<<*ri<<endl;
 }


 deque<int> b;
 b.push_back(4);
 b.push_back(5);
 b.push_front(3);
 b.push_front(2);
 cout<<"互换"<<endl;
 b.swap(a);
 for(int l=0;l<a.size();l++){
  cout<<a[l]<<endl;
 }
 return 0;
}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值