In C++, std::deque::resize() changes how many elements a std::deque holds. If the new size is bigger, new elements are added at the back. If it is smaller, extra elements are removed from the back.
New elements are filled with a default value (0 for numbers) or with a value that you choose. Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax
Pass the new size. You can pass a second argument to choose the value of any new elements.
C++
d.resize(5); // new elements are 0
d.resize(5, 9); // new elements are 9
d.resize(2); // keep only the first 2
Call
What it does
d.resize(5)
Makes the size 5. Missing elements are added at the back and set to T(), which is 0 for int.
d.resize(5, 9)
Makes the size 5. Missing elements are copies of 9.
d.resize(2)
Makes the size 2 by removing elements from the back.
Unlike std::vector, a deque has no reserve() or capacity(), so resize() is the way to set an exact element count.
Optional. The value used for new elements when the deque grows. If it is missing, new elements are value-initialized (0 for numbers).
Return Value
Return Value
Description
Nothing
resize() returns void. Check the result with size().
C++ Examples
Example 1 — Grow with resize()
Make a deque of three elements five elements long.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3};
d.resize(5);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << "(size " << d.size() << ")\n";
return 0;
}
Output
1 2 3 0 0 (size 5)
How It Works
The deque starts with the three values 1 2 3.
resize(5) needs two more elements, so it adds them at the back.
No value was given, so the new elements are set to 0. The old elements are kept.
Example 2 — Shrink with resize()
Make a deque shorter. The extra elements are dropped from the back.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {10, 20, 30, 40, 50};
d.resize(2);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << "(size " << d.size() << ")\n";
return 0;
}
Output
10 20 (size 2)
How It Works
The deque starts with five elements.
resize(2) keeps the first two elements and destroys the other three at the back.
The result is 10 20 with size 2. Elements from the front are never the ones removed.
Example 3 — Grow with a Chosen Value
Pass a second argument to fill the new elements with something other than zero.
C++
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2};
d.resize(5, 9);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
1 2 9 9 9
How It Works
The deque begins as 1 2, so three new elements are needed.
resize(5, 9) adds three copies of 9 at the back.
The old values 1 2 are not touched. The extra argument only affects the new elements.
Frequently Asked Questions
It changes the number of elements in the deque. A bigger size adds elements at the back, and a smaller size removes elements from the back.
If you pass no value, they are value-initialized, which is 0 for numbers and an empty object for classes such as std::string. You can pass a second argument to choose the value.
No. It only adds or removes at the back. The first elements always stay the same, unless the deque shrinks to a size of 0.
resize() changes the element count. reserve() only prepares memory and exists for vector, not for deque.
Nothing changes. The deque already has that many elements.