C++ Deque Methods

In C++, the std::deque class from the Standard Template Library (STL) provides many methods to manipulate a deque. Here are some of the most commonly used methods:

  1. push_back() and push_front(): These methods add an element to the back or front of the deque, respectively.
std::deque<int> d {1, 2, 3};
d.push_back(4); // add 4 to the back of the deque
d.push_front(0); // add 0 to the front of the deque
Sour‮www:ec‬.theitroad.com
  1. pop_back() and pop_front(): These methods remove the last or first element of the deque, respectively.
std::deque<int> d {1, 2, 3};
d.pop_back(); // remove the last element of the deque
d.pop_front(); // remove the first element of the deque
  1. at(): This method returns a reference to the element at a specified index. It throws an out_of_range exception if the index is out of bounds.
std::deque<int> d {1, 2, 3};
int x = d.at(1); // get the element at index 1 (x is 2)
  1. front() and back(): These methods return references to the first and last elements of the deque, respectively.
std::deque<int> d {1, 2, 3};
int first = d.front(); // get the first element of the deque (first is 1)
int last = d.back(); // get the last element of the deque (last is 3)
  1. size(): This method returns the number of elements in the deque.
std::deque<int> d {1, 2, 3};
int size = d.size(); // get the size of the deque (size is 3)
  1. empty(): This method returns true if the deque is empty, and false otherwise.
std::deque<int> d {1, 2, 3};
bool is_empty = d.empty(); // check if the deque is empty (is_empty is false)

These are just some of the many methods provided by the std::deque class in C++. You can consult the C++ reference documentation for a full list of deque methods and their descriptions.