0
0
CppHow-ToBeginner · 3 min read

How to Resize Vector in C++: Syntax and Examples

In C++, you can resize a vector using the resize() method, which changes the number of elements in the vector. If the new size is larger, new elements are added with a default or specified value; if smaller, elements at the end are removed.
📐

Syntax

The resize() method changes the size of a std::vector. It has two common forms:

  • vector.resize(new_size); - Resizes to new_size, new elements are default-initialized.
  • vector.resize(new_size, value); - Resizes and fills new elements with value.

If new_size is smaller than the current size, elements at the end are removed.

cpp
vector.resize(new_size);
vector.resize(new_size, value);
💻

Example

This example shows how to resize a vector to a larger size with a default value and then to a smaller size.

cpp
#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers = {1, 2, 3};
    std::cout << "Original vector size: " << numbers.size() << "\n";

    // Resize to larger size, new elements default to 0
    numbers.resize(5);
    std::cout << "After resizing to 5 (default values):\n";
    for (int n : numbers) {
        std::cout << n << " ";
    }
    std::cout << "\n";

    // Resize to larger size with specified value
    numbers.resize(7, 10);
    std::cout << "After resizing to 7 (value=10):\n";
    for (int n : numbers) {
        std::cout << n << " ";
    }
    std::cout << "\n";

    // Resize to smaller size
    numbers.resize(4);
    std::cout << "After resizing to 4 (smaller size):\n";
    for (int n : numbers) {
        std::cout << n << " ";
    }
    std::cout << "\n";

    return 0;
}
Output
Original vector size: 3 After resizing to 5 (default values): 1 2 3 0 0 After resizing to 7 (value=10): 1 2 3 0 0 10 10 After resizing to 4 (smaller size): 1 2 3 0
⚠️

Common Pitfalls

Common mistakes when resizing vectors include:

  • Assuming resize() changes the capacity; it only changes the size.
  • Not initializing new elements when resizing larger, leading to unexpected default values.
  • Accessing elements beyond the resized size causes undefined behavior.

Always check the vector size after resizing before accessing elements.

cpp
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {1, 2, 3};

    // Wrong: Accessing element beyond resized size
    v.resize(2);
    // std::cout << v[2]; // ERROR: out of range access

    // Right: Resize and access within bounds
    v.resize(4, 5); // new elements set to 5
    for (int i = 0; i < v.size(); ++i) {
        std::cout << v[i] << " ";
    }
    std::cout << "\n";

    return 0;
}
Output
1 2 5 5
📊

Quick Reference

Summary tips for resizing vectors:

  • resize(new_size): Changes size, new elements default-initialized.
  • resize(new_size, value): Changes size, new elements set to value.
  • Resizing smaller removes elements from the end.
  • Capacity may remain larger than size after resize.
  • Always check size before accessing elements.

Key Takeaways

Use vector.resize(new_size) to change the vector size safely.
New elements added by resize are default-initialized or set to a specified value.
Resizing smaller removes elements from the end of the vector.
Always check vector size after resizing before accessing elements.
resize() changes size but does not reduce vector capacity.