In C++, std::list::resize() changes a std::list so that it holds exactly n elements. If the list is too long, the extra elements at the end are erased. If it is too short, new elements are added at the end.
The new elements get a default value (0 for numbers, an empty string for strings) or a value you choose. Include <list> to use it.
This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.
Syntax
Call resize with the new size, and optionally the value for new elements.
C++
l.resize(5); // new elements are value-initialized (0 for int)
l.resize(4, -1); // new elements are copies of -1
l.resize(2); // shrink: erase from the end
Call
What it does
l.resize(n)
Makes the size n. New elements are value-initialized, so int gets 0.
l.resize(n, value)
Makes the size n. New elements are copies of value.
resize() never reorders anything. It only erases from the end or adds at the end. A list has no reserve(), because there is nothing to reserve.
Library declarations
C++
void resize(size_type n);
void resize(size_type n, const T& value);
Parameters
Parameter
Description
n
Required. The new number of elements.
value
Optional. The value copied into every new element when the list grows.
Return Value
Return Value
Description
Nothing
resize() returns void. The list now has exactly n elements.
C++ Examples
Example 1 — resize() to Make the List Longer
New elements of a number list are filled with 0.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l = {1, 2};
l.resize(5);
std::cout << "size: " << l.size() << '\n';
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
size: 5
1 2 0 0 0
How It Works
The list holds 1 2, so it has to grow by three elements.
resize(5) appends three new nodes. Numbers are value-initialized, which means they become 0.
The old elements 1 2 stay in front, and the three zeros are added at the end.
Example 2 — resize() to Make the List Shorter
The elements at the end are erased until only the wanted number is left.
C++
#include <iostream>
#include <iterator>
#include <list>
int main() {
std::list<int> l = {1, 2, 3, 4, 5};
auto second = std::next(l.begin()); // points at 2
l.resize(2);
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
std::cout << "*second: " << *second << '\n';
return 0;
}
Output
1 2
*second: 2
How It Works
second points at the node that holds 2.
resize(2) erases the last three nodes, 3 4 5. The first two nodes are not touched.
Because the node with 2 was not erased, *second still works and prints 2.
Example 3 — resize() with a Fill Value
Pass a second argument to choose what the new elements contain.
C++
#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> l = {"a"};
l.resize(3, "?");
for (const std::string& s : l) {
std::cout << s << ' ';
}
std::cout << '\n';
return 0;
}
Output
a ? ?
How It Works
The list holds one string, "a", and it needs two more to reach size 3.
resize(3, "?") appends two new strings that are copies of "?".
Without the second argument the new strings would be empty. With it, the output is a ? ?.
Frequently Asked Questions
It changes the size of the list to n. It erases elements from the end if the list is too long, or appends new elements if the list is too short.
If you give no value, they are value-initialized: 0 for numbers, false for bool, an empty string for std::string. With a second argument they are copies of that value.
The last ones. resize(2) keeps the first two elements and erases all the others.
Only iterators to erased elements become invalid. When the list grows, no existing iterator is invalidated.
No. Every element lives in its own node, so there is no spare memory to reserve or give back.