Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements. Unlike vectors, contiguous storage allocation may not be guaranteed. 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 than vectors, but this allows them to grow more efficiently under certain circumstances, especially with very long sequences, where reallocations become more expensive.

Syntax

deque< object_type > deque_name;

 

Member Functions

  • void push_back (const value_type& val);
    void push_back (value_type&& val);
    Adds a new element at the end of the deque container, after its current last element. The content of val is copied (or moved) to the new element.
  • void push_front (const value_type& val);
    void push_front (value_type&& val);
    Inserts a new element at the beginning of the deque container, right before its current first element. The content of val is copied (or moved) to the inserted element.
  • void pop_back();
    Removes the last element in the deque container, effectively reducing the container size by one.
  • void pop_front();
    Removes the first element in the deque container, effectively reducing its size by one.
  • size_type size() const noexcept;
    Returns the number of elements in the deque container.
  • void swap (deque& x);
    Exchanges the content of the container by the content of x, which is another deque object containing elements of the same type. Sizes may differ.
  • reference back();
    const_reference back() const;
    Returns a reference to the last element in the container.
  • reference front();
    const_reference front() const;
    Returns a reference to the first element in the deque container.
  • iterator begin() noexcept;
    const_iterator begin() const noexcept;
    Returns an iterator pointing to the first element in the deque container.
  • iterator end() noexcept;
    const_iterator end() const noexcept;
    Returns an iterator referring to the past-the-end element in the deque container.

 

Example

#include <iostream> 
#include <deque> 

using namespace std; 

int main() 
{ 
  // Creates deque with elements 1,5,8,9,3
  int a[] = { 1,5,8,9,3 };
  deque<int> dq(a, a+5);
    
  deque <int> d1; 
  
  // Insert element
  d1.push_back(10); 
  d1.push_front(20); 
  d1.push_back(30); 
  d1.push_front(15); 

  // Print deque
  deque <int> :: iterator it; 
  for (it = d1.begin(); it != d1.end(); ++it) 
    cout << *it; 

  // Size of deque
  cout << d1.size(); 

  // Element at index 2
  cout << d1.at(2); 

  // Change the value of element at 0
  d1[0] = 10;
    
  // Element at front and back
  cout << d1.front(); 
  cout << d1.back(); 

  // Remove element from front and back
  d1.pop_front(); 
  d1.pop_back(); 
  
  std::deque<int>::iterator it = d1.begin();

  it = mydeque.begin()+2;

  std::vector<int> vec (2,30);
  d1.insert (it, vec.begin(), vec.end());

  return 0; 
}