C++ Standard Library C++ STL Library

C++ - <deque>



C++ <deque>

Deque stands for Double Ended Queue. A deque is a sequence container with dynamic size and it can be expanded or contracted from both sides (front side or back side). The size of a deque changes automatically when elements are appended or deleted. Along with this, it stores elements of same data types and strictly in linear sequence.

Unlike vector, a deque is not guaranteed to store elements in contiguous memory location, hence it does not support direct access of data by offsetting pointers. But it enables direct access of any elements using operator []. Deque provides similar functionality as vector, but provides efficient way to insert or delete data from any end.

Syntax

template < class T, class Alloc = allocator<T> > class deque;

Parameters

T Type of the elements stored in the container.
Alloc Type of the allocator object used to define the storage allocation model, default: allocator.

Member Types

Member typesDefinition
value_type T (First template parameter)
allocator_type Alloc (Second template parameter), default: allocator<value_type>
reference value_type&
const_reference const value_type&
pointer Alloc::pointer, default: value_type*
const_pointer Alloc::const_pointer, default: value_type*
iterator a random access iterator to value_type
const_iterator a random access iterator to const value_type
reverse_iterator reverse_iterator <iterator>
const_reverse_iterator reverse_iterator <const_iterator>
difference_type ptrdiff_t
size_type size_t

C++ <deque> - Member Functions

The C++ deque container has a number of member functions which are listed below:

FunctionsDescription
deque() Construct a deque object.
~deque() Destroys container by deallocating container memory.
operator=() Assign content to a deque.

Capacity

FunctionsDescription
empty() Checks whether the deque is empty or not.
size() Returns the length of the deque in terms of bytes.
max_size() Returns the maximum length of the deque.
resize() Changes the size of the deque by specified number of elements.
shrink_to_fit() Reduces the capacity of the deque equal to fit its size.

Element Access

FunctionsDescription
at() Access an element of the deque.
operator[]() Access an element of the deque.
front() Access first element of the deque.
back() Access last element of the deque.

Iterators

FunctionsDescription
begin() Returns iterator pointing to the first element of the deque.
end() Returns iterator pointing to the past-the-last element of the deque.
rbegin() Returns reverse iterator to the last element of the deque.
rend() Returns reverse iterator to the element preceding the first element of the deque.
cbegin() Returns const_iterator pointing to the first element of the deque.
cend() Returns const_iterator pointing to the past-the-last element of the deque.
crbegin() Returns const_reverse_iterator to the last element of the deque.
crend() Returns const_reverse_iterator to the element preceding the first element of the deque.

Modifiers

FunctionsDescription
assign() Assign deque content.
clear() Clears all elements of the deque.
pop_front() Deletes first element of the deque.
push_front() Adds a new element at the beginning of the deque.
pop_back() Deletes last element of the deque.
push_back() Adds a new element at the end of the deque.
insert() Insert elements in the deque.
erase() Deletes either a single element or range of elements from a deque.
emplace() Constructs and inserts a new element at specified position in the deque
emplace_front() Constructs and inserts a new element at the beginning of the deque.
emplace_back() Constructs and inserts a new element at the end of the deque.
swap() Exchanges elements between two deques.

Allocator

FunctionsDescription
get_allocator() Return a copy of allocator object associated with the deque.

C++ <deque> - Non-member Functions

FunctionsDescription
operator == Checks whether two deques are equal or not.
operator != Checks whether two deques are unequal or not.
operator < Checks whether the first deque is less than the other or not.
operator > Checks whether the first deque is greater than the other or not.
operator <= Checks whether the first deque is less than or equal to the other or not.
operator >= Checks whether the first deque is greater than or equal to the other or not.
swap() Exchanges elements between two deques.