Create C++ STL Deque
In C++, you can create a deque using the std::deque
template class from the Standard Template Library (STL). Here is an example of how to create a deque:
#include <deque> #include <iostream> int main() { std::deque<int> d {1, 2, 3, 4, 5}; // create a deque with 5 elements d.push_back(6); // add an element to the back of the deque d.push_front(0); // add an element to the front of the deque for (int i : d) { std::cout << i << " "; // output the elements of the deque } std::cout << std::endl; return 0; }Source:w.wwtheitroad.com
Output:
0 1 2 3 4 5 6
In this example, we include the <deque>
header file to use the std::deque
template class. We then create a deque d
with five elements using the initialization list syntax. We add an element to the back of the deque using the push_back()
method, and an element to the front of the deque using the push_front()
method. Finally, we use a range-based for
loop to output the elements of the deque to the console.
You can also create an empty deque and add elements to it later using the push_back()
and push_front()
methods, or the emplace_back()
and emplace_front()
methods to construct and insert elements in place.