C++ STL Deque Container – std::deque

Here you will learn about C++ STL Deque container i.e. std::deque and all functions applicable on it.

Note: Deque should be pronounced as “deck”.

It named because Double Ended Queue (DEQUE). Deques are come under sequence containers. These are double ended with features of expansion and contraction on both the ends. These are similar to vectors. But more efficient than vectors in case of insertion and deletion of elements not only at end but also at the beginning of the sequence. But here contiguous memory allocation may not be guaranteed.

Also Read: C++ STL Vector Container – std::vector

C++ STL Deque

To use deque we must include its header <deque> i.e. #include<deque>

Different Syntax for Declaring Deque

Creating an empty deque:

Creating a deque with 10 empty elements:

Creating a deque with 10 elements, each element have value 3:

Array to deque:

Copying all deque elements to another deque:

Output

10 10 10 10 10
10 10 10 10 10

Inserting Elements Into Deque

push_back(element): This inserts the element at the end of the deque.

push_front(element): This function inserts the element at the front of the deque.

insert(): insert() function can be used in different ways.

  • We can insert an element at particular position pointed by the iterator. For this we use two                  arguments. Those are (iterator, value to be inserted) respectively.
  • We can insert an element, “n” no. of times at front of the deque. For this we use three arguments. Those are (iterator, number n, inserted value) respectively.
  • We can insert array elements form particular index to another index. For this we use three arguments, (iterator, arrayStartIndex, arrayLastIndex);

assign(): assign (num, value), this inserts value into deque num times.

Example program to show different ways of inserting element into deque:

Output

enter an element to insert at back
10
enter an element to insert at front
15
inserting element 15 at start of the deque using iterator
inserting element 10, two times at end of the deque
Inserting first 3 elemtns of the array(1,2,3) to at the fornt of the deque
firnal result is
1 2 3 15 15 10 10 10 using assign inserting into new deque
New deque elements are
99 99 99 99 99

Deleting Elements form Deque

pop_back(): This will deletes the last element of the deque.

pop_front(): This will deletes the first element of the deque.

erase(): This function deletes the element which pointed by the iterator at particular position.

clear(): This function removes all elements from the deque.

Example program to show different ways of deleting an element form deque:

Output

Initially deque contains elements are
0 1 2 3 4 5 6
Deleting last element using pop_back
Deleting first element using pop_fornt
deleting elements at index 2
Resultant deque upto now->
1 2 4 5
using clear function
finally deque is empty

resize(): Resize can be applied for increasing or decreasing the current size of the deque.

size(): Returns an integer that the number of elements present in the deque

Max_size(): Returns a system and architecture dependent value.

empty(): This is Boolean function, that returns true if deque is empty returns false if it’s not empty.

swap(): It swaps the all elements of deque1 to deque2. And all values of deque2 to deque1.

Example program to demonstrate above functions:

Output

deque size is 5
deque size after resize is 3
maxsize of deque is 4611686018427387903
deque is not empty
Elements of the deque1 before swap
10 10 10 10 10
Elements of the deque2 before swap
20 20 20 20 20
Elements of the deque1 after swap
20 20 20 20 20
Elements of the deque2 after swap
10 10 10 10 10

Comment below if you have queries or found any information incorrect in above tutorial for C++ STL Deque.

Leave a Reply

Your email address will not be published. Required fields are marked *