C++ std::list::resize
The std::list::resize
function adjusts the size of a std::list
. If the new size is greater than the current size, new elements are added to the list, and these new elements are value-initialized. If the new size is smaller, elements are removed from the end of the list.
Syntax of std::list::resize
void resize(size_type n);
void resize(size_type n, const value_type& val);
Parameters
Parameter | Description |
---|---|
n | The new size of the list. |
val | (Optional) The value to initialize new elements if the list is resized to a larger size. |
Return Value
This function does not return a value. It modifies the size of the list in place.
Exceptions
The std::list::resize
function may throw exceptions if memory allocation fails or if the copy or move constructor of the new elements throws an exception. The function provides strong exception safety: if an exception is thrown, the state of the list remains unchanged.
Examples for std::list::resize
Example 1: Resizing to a Larger Size with Default Values
This example demonstrates how to use resize
to increase the size of a list with default-initialized values:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {10, 20, 30};
// Resize the list to contain 5 elements
myList.resize(5);
std::cout << "List contents after resize: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is initialized with the elements{10, 20, 30}
. - The
resize
function is called with a new size of5
. - Two new elements are added to the list, and they are default-initialized to
0
. - A range-based
for
loop iterates through the list and prints its contents.
Output:
List contents after resize: 10 20 30 0 0
Example 2: Resizing to a Larger Size with a Specific Value
This example demonstrates how to use resize
to increase the size of a list with a specific value:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {1, 2, 3};
// Resize the list to contain 6 elements, with new elements initialized to 42
myList.resize(6, 42);
std::cout << "List contents after resize: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is initialized with the elements{1, 2, 3}
. - The
resize
function is called with a new size of6
and a value of42
. - Three new elements are added to the list, each initialized to
42
. - A range-based
for
loop iterates through the list and prints its contents.
Output:
List contents after resize: 1 2 3 42 42 42
Example 3: Resizing to a Smaller Size
This example demonstrates how to use resize
to reduce the size of a list:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {100, 200, 300, 400, 500};
// Resize the list to contain only 3 elements
myList.resize(3);
std::cout << "List contents after resize: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is initialized with the elements{100, 200, 300, 400, 500}
. - The
resize
function is called with a new size of3
. - The last two elements (
400
and500
) are removed from the list. - A range-based
for
loop iterates through the list and prints its contents.
Output:
List contents after resize: 100 200 300