A std::deque (short for "double-ended queue") is a container class in the C++
Standard Template Library (STL) that stores a dynamically-sized array of elements.
Like a std::vector, a std::deque provides automatic memory management and dynamic
resizing, but it also allows for efficient insertion and removal of elements at
both the front and back of the container.
Here is an example of creating and using a std::deque:
Copy code
#include <deque>
#include <iostream>
int main() {
// Create a deque of integers
std::deque<int> myDeque;
// Add the numbers 1-5 to the front of the deque
for (int i = 1; i <= 5; i++) {
myDeque.push_front(i);
}
// Print the deque
for (int i = 0; i < myDeque.size(); i++) {
std::cout << myDeque[i] << " ";
}
std::cout << std::endl;
// Add the number 6 to the back of the deque
myDeque.push_back(6);
// Print the deque
for (int i = 0; i < myDeque.size(); i++) {
std::cout << myDeque[i] << " ";
}
std::cout << std::endl;
// Remove the first element from the deque
myDeque.pop_front();
// Print the deque
for (int i = 0; i < myDeque.size(); i++) {
std::cout << myDeque[i] << " ";
}
std::cout << std::endl;
// Remove the last element from the deque
myDeque.pop_back();
// Print the deque
for (int i = 0; i < myDeque.size(); i++) {
std::cout << myDeque[i] << " ";
}
std::cout << std::endl;
return 0;
}
This program creates a std::deque called myDeque. It then uses a for loop to add
the numbers 1-5 to the front of the deque using the push_front() function. After
that it will print the deque. Then it will add the number 6 to the back of the
deque using the push_back() function. After that it will print the deque again,
and finally it will remove the first and last element from the deque using the
pop_front() and pop_back() functions respectively, and the final output will be
the deque containing numbers 2-5.
As we can see, the difference between std::vector and std::deque is that a
std::deque allows for efficient insertion and removal of elements at both the
front and back of the container, whereas std::vector only allows insertion and
removal of elements at the back.
To embed this project on your website, copy the following code and paste it into your website's HTML: