STL Deque in C++

The deque (short for double-ended queue) is a dynamic array-like container provided by the C++ STL that allows efficient insertion and removal of elements at both ends.

Necessary header file
#include<deque>
Declaration of deque
std::deque<DataType> myDeque;

Replace DataType with the actual data type you want to store in the deque.

Insert elements into the deque

You can use the push_back and push_front member functions to add elements at the end and beginning of the deque, respectively.

myDeque.push_back(element);   // Insert element at the end
myDeque.push_front(element);  // Insert element at the beginning
Accessing elements in the deque

You can use the square bracket [] operator or the at member function to access elements in the deque by their index. The index starts from 0 for the first element.

// Using the square bracket operator
DataType value = myDeque[index];
// Using the at() member function
DataType value = myDeque.at(index);
Removing elements from the deque

To remove elements, you can use the pop_back and pop_front member functions to remove the last and first elements, respectively.

myDeque.pop_back();      // Remove the last element
myDeque.pop_front();     // Remove the first element
Other functions and operations
size()

This member function returns the number of elements in the deque. It provides the current size of the deque by counting the number of elements present.

Example
std::deque<int> myDeque = {1, 2, 3, 4, 5};
int size = myDeque.size(); // size is 5
empty()
This member function checks whether the deque is empty or not. It returns true if the deque is empty (contains no elements), and false otherwise.

Example
std::deque<int> myDeque;
bool isEmpty = myDeque.empty(); // isEmpty is true
clear()

This member function removes all elements from the deque, leaving it empty. It effectively clears the deque by removing all elements. After calling clear(), the deque will have a size of 0.

Example
std::deque myDeque = {1, 2, 3, 4, 5};
myDeque.clear(); // The deque is now empty
insert()

This member function inserts elements at a specified position in the deque. It takes an iterator specifying the position and the value to be inserted. The iterator can be obtained using the begin(), end(), or other iterator functions of the deque.

Example
std::deque<int> myDeque = {1, 2, 3, 4, 5};
std::deque<int>::iterator it = myDeque.begin() + 2; // Insert at index 2
myDeque.insert(it, 10); // Insert 10 at index 2
erase()

This member function removes elements from a specified position or a range of positions in the deque. It takes an iterator specifying the position or range to be removed. The iterator can be obtained using the begin(), end(), or other iterator functions of the deque.

Example
std::deque<int> myDeque = {1, 2, 3, 4, 5};
std::deque<int>::iterator it = myDeque.begin() + 2; // Remove element at index 2
myDeque.erase(it); // Remove element at index 2
std::deque<int>::iterator start = myDeque.begin() + 1; // Remove range from index 1 to 3
std::deque<int>::iterator end = myDeque.begin() + 4;
myDeque.erase(start, end); // Remove range from index 1 to 3
resize()

This member function changes the size of the deque by either truncating or adding elements. It takes an argument specifying the new size of the deque. If the new size is smaller than the current size, elements are removed from the end. If the new size is larger, new elements are added at the end with a default-initialized value.

Example
std::deque<int> myDeque = {1, 2, 3, 4, 5};
myDeque.resize(3); // Resize the deque to 3 elements (truncates)
myDeque.resize(7, 0); // Resize the deque to 7 elements (adds 0)
swap()

This member function swaps the contents of two deques. It takes another deque as an argument and swaps the elements between the two deques. After the swap, the original deques have each other's elements.

Example
std::deque<int> deque1 = {1, 2, 3};
std::deque<int> deque2 = {4, 5, 6};
deque1.swap(deque2); // Swap the contents of deque1 and deque2