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:
push_back()
andpush_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 dequeSourwww:ec.theitroad.com
pop_back()
andpop_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
at()
: This method returns a reference to the element at a specified index. It throws anout_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)
front()
andback()
: 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)
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)
empty()
: This method returnstrue
if the deque is empty, andfalse
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.