双端队列 の deque

在www.cplusplus.com中指出它是一个容器----和向量vector,栈stack,队列queue一样。

当然,标准C++库中的容器有:<array>、<bitset>、<deque>、<forward_list>、<list>、<map>、<queue>、<set>、<stack>、<unordered_map>、
<unordered_set>、<vector>


deque  ==>  double end queue  (发音 deck )是个模板类;它有两个模板函数: begin 和 end。 分别用作 iterator to begin & iterator to end....

双端队列是一个在两端都可以扩展和收缩的线性容器。Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back).


Specific libraries may implement deques in different ways, generally as some form of dynamic array. But in any case, they allow for the individual elements to be accessed directly through random access iterators, with storage handled automatically by expanding and contracting the container as needed.

双端队列的功能和向量类似,但是不仅仅是在尾部增加和删除效率较高,而且在序列的首部增加或删除的效率也很高。Therefore, they provide a functionality similar tovectors, but with efficient insertion and deletion of elements also at the beginning of the sequence, and not only at its end. But, unlikevectors, deques are not guaranteed to store all its elements in contiguous storage locations: accessing elements in adeque by offsetting a pointer to another element causes undefined behavior.
与向量不同的是,双端队列元素存储并不是在一个连续的存储空间内,通过指针偏移访问双端队列中的另一个元素会导致意想不到的结果(往往是坏的结果)。

Both vectors and deques provide a very similar interface and can be used for similar purposes, but internally both work in quite different ways: While vectors use a single array that needs to be occasionally reallocated for growth, the elements of a deque can be scattered in different chunks of storage, with the container keeping the necessary information internally to provide direct access to any of its elements in constant time and with a uniform sequential interface (through iterators). Therefore, deques are a little more complex internally thanvectors, but this allows them to grow more efficiently under certain circumstances, especially with very long sequences, where reallocations become more expensive.
虽然向量和双端队列为实现相同的操作提供了类似的接口。但是内部实现方式各不相同。 向量使用的是一个数组,在序列增长时,向量中的数组可能需要被重新分配(size>capacity)。双端队列的元素可以分散在不同的存储块中,容器在内部则保持者必要的信息,保证在一个常数时间内可以直接访问任何一个元素(常数时间内 或者 时间常数内 比如:0.1s以内),而且是一个统一的序列接口迭代器)。因此,双端队列的内部实现要比向量更复杂。但是这使得双端队列在相同情况下增加操作更具高效性。尤其当序列非常长时,重新分配的代价往往很昂贵(向量增加时当size>capacity时,需要使用new的方法重新申请存储空间)。

For operations that involve frequent insertion or removals of elements at positions other than the beginning or the end, deques perform worse and have less consistent iterators and references thanlists and forward lists.
对于在除首部和尾部其他位置,涉及频繁插入和删除,相对于list和 forward list 来说,双端队列则稍逊一筹,而且很少有较为一直的迭代器和引用。

双端队列有这样一些操作:

push_back 尾部插入
push_front 首部插入
pop_back 尾部删除
pop_front 首部删除
 
insert 指定位置插入
erase 指定位置删除
clear 清空
   
front 获取头部元素
end 获取尾部元素
at 获取指定位置元素
   
begin 迭代器指向首部
end 的带起指向尾部下一个元素

当然还有其他的一些操作,如: 获取双端队列的大小size,最大大小max_size,判空empty,   deque1.swap(deque2)交换两个双端队列的内容(尺寸可以不一样,但是内容的类型必须相同,如:同int,同double),还有最重要的就是 容器 间相互复制assign,赋值operator=。

// deque::assign
#include <iostream>
#include <deque>

int main ()
{
  std::deque<int> first;
  std::deque<int> second;
  std::deque<int> third;

  first.assign (7,100);             // 7 ints with a value of 100

  std::deque<int>::iterator it;
  it=first.begin()+1;

  second.assign (it,first.end()-1); // the 5 central values of first

  int myints[] = {1776,7,4};
  third.assign (myints,myints+3);   // assigning from array.

  std::cout << "Size of first: " << int (first.size()) << '\n';      // 7
  std::cout << "Size of second: " << int (second.size()) << '\n';    // 5
  std::cout << "Size of third: " << int (third.size()) << '\n';      // 3
  return 0;
}

// assignment operator with deques
#include <iostream>
#include <deque>

int main ()
{
  std::deque<int> first (3);    // deque with 3 zero-initialized ints
  std::deque<int> second (5);   // deque with 5 zero-initialized ints

  second = first;
  first = std::deque<int>();

  std::cout << "Size of first: " << int (first.size()) << '\n';    //  <span id="result_box" lang="zh-CN"><span></span></span>first = std::deque<int>(); ==> 0
  std::cout << "Size of second: " << int (second.size()) << '\n';  //  0
  return 0;<span id="result_box" lang="zh-CN"></span>
更多请参阅 http://www.cplusplus.com/reference/deque/deque/



你可能感兴趣的:(数据结构,C++,双端队列,deque)