C++ Standard Library C++ STL Library

C++ <deque> - deque() Function



The C++ deque::deque function is used to construct a deque object, initializing its contents depending on the version of constructor used:

Syntax

//default version - construct an empty 
//container with no elements
explicit deque (const allocator_type& alloc = allocator_type());

//fill version - construct a container 
//with n elements, each equal to val
explicit deque (size_type n, const value_type& val = value_type(),
                const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
    deque (InputIterator first, InputIterator last,
           const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of x into the container	
deque (const deque& x);
//default version - construct an empty 
//container with no elements
explicit deque (const allocator_type& alloc = allocator_type());

//fill version - construct a container 
//with n elements, each equal to val	
explicit deque (size_type n);
deque (size_type n, const value_type& val,
       const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  deque (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of x into the container	
deque (const deque& x);
deque (const deque& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
deque (deque&& x);
deque (deque&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container	
deque (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());
//default version - construct an empty 
//container with no elements
deque();
explicit deque (const allocator_type& alloc);

//fill version - construct a container 
//with n elements, each equal to val
explicit deque (size_type n, const allocator_type& alloc = allocator_type());
deque (size_type n, const value_type& val,
       const allocator_type& alloc = allocator_type());

//range version - Constructs a container with 
//elements as the range [first,last)
template <class InputIterator>
  deque (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());

//copy version - copies all elements
//of x into the container	
deque (const deque& x);
deque (const deque& x, const allocator_type& alloc);

//move version - moves elements of x
//into the container	
deque (deque&& x);
deque (deque&& x, const allocator_type& alloc);

//initializer list version - copies all 
//elements of il into the container		
deque (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());

Parameters

alloc Specify the Allocator object. The container keeps and uses an internal copy of this allocator.
n Specify the number of elements in the container at construction.
val Specify the value to fill the container with.
first Specify initial position of the input iterator of the range. The range used is [first,last).
last Specify final position of the input iterator of the range. The range used is [first,last).
x Specify a deque object of same type.
il Specify an initializer_list object.

Return Value

Constructor never returns value.

Time Complexity

Constant i.e, Θ(1), for default version and move version.
For all the other cases, Linear i.e, Θ(n).

Example: using default and fill version

In the example below, the deque::deque function is used to construct a deque object.

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  //default version - construct an empty deque
  deque<int> dq1;
  deque<int>::iterator it;

  //fill version - construct a deque
  //with 5 elements each equal to 100
  deque<int> dq2(5, 100);

  cout<<"dq1 contains: ";
  for(it = dq1.begin(); it != dq1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\ndq2 contains: ";
  for(it = dq2.begin(); it != dq2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

dq1 contains: 
dq2 contains: 100 100 100 100 100 

Example: using range and copy version

A deque can also be constructed using range or copy version. Consider the following example:

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> dq1{100, 200, 300};
  deque<int>::iterator it;

  //range version - construct dq2 using range
  deque<int> dq2(dq1.begin(), dq1.end());

  //copy version - construct dq3 from dq1
  deque<int> dq3(dq1); 

  cout<<"dq2 contains: ";
  for(it = dq2.begin(); it != dq2.end(); ++it)
    cout<<*it<<" ";

  cout<<"\ndq3 contains: ";
  for(it = dq3.begin(); it != dq3.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

dq2 contains: 100 200 300
dq3 contains: 100 200 300 

Example: using move version

Using the move version of deque, the content of one deque can be moved to another deque. Consider the following example:

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  deque<int> dq1{10, 20, 30, 40, 50};
  deque<int>::iterator it;

  cout<<"dq1 contains: ";
  for(it = dq1.begin(); it != dq1.end(); ++it)
    cout<<*it<<" ";

  //moving all content of dq1 into dq2
  deque<int> dq2(move(dq1));

  cout<<"\ndq1 contains: ";
  for(it = dq1.begin(); it != dq1.end(); ++it)
    cout<<*it<<" ";

  cout<<"\ndq2 contains: ";
  for(it = dq2.begin(); it != dq2.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

dq1 contains: 10 20 30 40 50 
dq1 contains: 
dq2 contains: 10 20 30 40 50 

Example: using initializer list version

The initializer list can also be used to assign values into a deque container. Consider the example below:

#include <iostream>
#include <deque>
using namespace std;
 
int main (){
  //creating initializer list
  initializer_list<int> ilist = {15, 30, 45, 60, 75};

  //initializer list version - copies all 
  //elements of ilist into the container	
  deque<int> dq(ilist);
  deque<int>::iterator it;

  cout<<"dq contains: ";
  for(it = dq.begin(); it != dq.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

dq contains: 15 30 45 60 75 

❮ C++ <deque> Library