The choice between std::deque
and std::vector
depends on your specific use case. Here are some guidelines:
Use a std::deque
 when:
std::vector
Use a std::vector
 when:
Here's an example demonstrating the performance difference for inserting at the front:
#include <deque>
#include <vector>
#include <iostream>
#include <chrono>
int main() {
using std::chrono::high_resolution_clock;
const int size = 10000;
auto start = high_resolution_clock::now();
std::deque<int> d;
for (int i = 0; i < size; ++i) {
d.emplace_front(i);
}
auto end = high_resolution_clock::now();
auto dequeTime = end - start;
start = high_resolution_clock::now();
std::vector<int> v;
for (int i = 0; i < size; ++i) {
v.emplace(v.begin(), i);
}
end = high_resolution_clock::now();
auto vectorTime = end - start;
std::cout
<< "Deque time: " << dequeTime.count()
<< "\nVector time: " << vectorTime.count();
}
Deque time: 1695200
Vector time: 6918000
The deque insertion is much faster because it's optimized for this operation.
Answers to questions are automatically generated and may not have been reviewed.
std::deque
A guide to double-ended queues - a structure that behaves like a vector, specialised for manipulating objects at the edges of the collection