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

</>
Copy
void resize(size_type n);
void resize(size_type n, const value_type& val);

Parameters

ParameterDescription
nThe 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

</>
Copy
#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:

  1. A std::list named myList is initialized with the elements {10, 20, 30}.
  2. The resize function is called with a new size of 5.
  3. Two new elements are added to the list, and they are default-initialized to 0.
  4. 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

</>
Copy
#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:

  1. A std::list named myList is initialized with the elements {1, 2, 3}.
  2. The resize function is called with a new size of 6 and a value of 42.
  3. Three new elements are added to the list, each initialized to 42.
  4. 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

</>
Copy
#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:

  1. A std::list named myList is initialized with the elements {100, 200, 300, 400, 500}.
  2. The resize function is called with a new size of 3.
  3. The last two elements (400 and 500) are removed from the list.
  4. A range-based for loop iterates through the list and prints its contents.

Output:

List contents after resize: 100 200 300