c++deque详解

STL deque容器

一、简单介绍

​ $ deque$ 的存储空间并不是连续的。内部的实现通常是一种特殊的数据结构,被称为分段数组。允许在两端进行高效的插入与删除操作,同时还支持随机访问。(支持随机访问是因为 d e q u e deque deque 维护了一个中央目录,其中存储了指向每个块的指针)

二、定义与使用
  • 创建一个没有任何元素的空deque容器

    • std::deque<int> d
      
  • 创建一个具有n个元素,默认值为m的deque

    • std::deque<int> d(n,m);
      
  • 通过拷贝的方式创建一个新的deque

    • std::deque<int> d2(d1);   //d1是以及存在的deque
      
  • 拷贝其他类型的指定区域的元素

    • //拷贝普通数组,创建deque容器
      int a[] = { 1,2,3,4,5 };
      std::deque<int>d(a, a + 5);
      //适用于所有类型的容器
      std::array<int, 5>arr{ 11,12,13,14,15 };
      std::deque<int>d(arr.begin()+2, arr.end());//拷贝arr容器中的{13,14,15}
      
三、遍历方式
  • 索引for循环

    • for(std::size_t i=0;i<mydeque ;i++){
      	std::cout<<mydeque[i]<<std::endl;
      }
      
  • 基础for循环

    • for(const auto & i:mydeque){
      	std::cout<<i<<std::endl;
      }
      
  • 迭代器 for遍历

    • for(auto it=mydeque.begin();it!=mydeque;it++){
      	std::cout<<*it<<std::endl;
      }
      
四、相关函数
  • a s s i g n assign assign 函数

    • void assign(InputIterator First,InputIterator Last)
      //First 要从参数deque中复制一系列元素的第一个元素
      //Last 超出deque中复制一系列元素的第一个元素
      
    • void assign(size_type Count,const Type& Val);
      //Count 要插入deque中的元素副本的个数
      // 插入到deque中的元素的值
      
    • void assign(initializer_list <Type> IList);
      //IList 要插入deque中的initializer_list
      
  • a t at at 函数

    • reference at(size_type pos);
      const_reference at(size_type pos)//返回deque中指定位置的元素的引用
      //溢出抛出std::out_of_range 的异常
      
  • b a c k back back 函数

    • reference back();
      const_reference back() const;//返回对deque中最后一个元素的引用
      
  • f r o n t front front 函数

    • reference front();
      const_reference front() const;//返回对deque中第一个元素的引用
      
  • b e g i n begin begin 函数

    • const_iterator begin() const;
      iterator begin();//发现 deque 中第一个元素或空 deque 之后的位置的随机访问迭代器
      
  • e n d end end 函数

    • const_iterator end() const;
      iterator end();
      
  • c b e g i n cbegin cbegin 函数

    • const_iterator cbegin() const; //返回确定范围中第一个元素地址的 const 迭代器
      
  • c e n d cend cend 函数

    • const_iterator cend() const; //返回一个 const 迭代器,此迭代器用于发现刚超出范围中最后一个元素的位置
      
  • c l e a r clear clear 函数

    • void clear();//清除deque所有函数
      
  • e m p l a c e emplace emplace 将构造的元素放到指定位置的 d e q u e deque deque

    • iterator emplace(
          const_iterator _Where,
          Type&& val);
      // _where 插入第一个元素的位置
      // val 插入到deque 中的元素的值
      //该函数将返回一个指向 deque 中新元素的插入位置的迭代器。
      
  • e m p l a c e _ b a c k emplace\_back emplace_back 函数

    • void emplace_back(Type&& val);// val 添加到 deque 末尾的元素
      
  • e m p l a c e _ f r o n t emplace\_front emplace_front 函数

    • void emplace_front(Type&& val);  //val 要添加到 deque 开头的元素
      
  • e m p t y empty empty 函数

    • bool empty() const;  //如果 deque 为空,则为 true;如果 deque 不为空,则为 false
      
  • e r a s e erase erase 函数

    • iterator erase(iterator _Where);
      iterator erase(iterator first, iterator last);
      // _where 要从 deque 中移除的元素的位置
      // first 要从 deque 中移除的第一个元素的位置
      //last 要从 deque 中移除的刚超出最后一个元素的位置
      
  • $insert $ 函数

    • iterator insert(
          const_iterator Where,
          const Type& Val);
      
      iterator insert(
          const_iterator Where,
          Type&& Val);
      // && 在这里就是使用move()函数进行传参,减少不必要空间的浪费
      
      void insert(
          iterator Where,
          size_type Count,
          const Type& Val);
      
      template <class InputIterator>
      void insert(
          iterator Where,
          InputIterator First,
          InputIterator Last);
      
      iterator insert(
          iterator Where,initializer_list<Type>
      IList);
      
  • m a x _ s i z e max\_size max_size 函数

    • size_type max_size() const; //返回deque的最大长度
      
  • p o p _ b a c k pop\_back pop_back 函数

    • void pop_back(); //删除deque末尾处的元素
      
  • p o p _ f r o n t pop\_front pop_front 函数

    • void pop_front(); //删除deque开头的元素
      
  • p u s h _ b a c k push\_back push_back 函数

    • void push_back(const Type& val);
      
      void push_back(Type&& val);//将元素添加到deque的末尾
      
  • p u s h _ f r o n t push\_front push_front 函数

    • void push_front(const Type& val);
      void push_front(Type&& val);   //将元素添加到开头
      
  • r e s i z e resize resize 函数

    • void resize(size_type _Newsize);
      
      void resize(size_type _Newsize, Type val);//为deque指定大小
      
  • s i z e size size 函数

    • size_type size() const; //返回deque中的元素数量 
      
  • s w a p swap swap 函数

    • void swap(deque<Type, Allocator>& right); //直接交换两个队列
      
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值