What is the vector::resize() function in C++?

In this shot, we will learn about the vector::resize() function in C++.

Introduction

The vector::resize() function is available in the <vector> header file in C++.

The vector::resize() function resizes the vector container so that it contains memory space to store nn elements, where nn is the parameter passed to the function.

If nn is greater than the current vector container size, then the content is expanded by inserting as many memory spaces that are needed to reach a size of nn at the end. If we pass any particular value that we want our newly allocated vector container to be initialized with, then all the newly assigned memory spaces will hold that value. If no particular value is given, then the memory spaces will hold a value of 00.

For example, if we have a vector that contains {2, 3, 4} and we call resize(5), which changes the size of the vector container to 55 and initializes new values by 00. The vector will be {2, 3, 4, 0, 0}. However, if we call resize(5, 20), then it will change the size of the vector container to 55 and initialize new elements with 2020. The resultant vector will be {2, 3, 4, 20, 20}.

If the value of nn is smaller than the current vector container size, then the container size is decreased to its nn elements from starting, and it removes the extra ending elements and destroys them.

Syntax

The syntax of the vector::resize() function is given below:

void resize(size_type n, value_type val = value_type());

Parameters

  • size: This parameter is of type unsigned integer and specifies the size of the vector container.

  • initialization_val: This is an optional parameter of the same type as the elements in the vector. The default value is the value of that type. For example, if the vector contains integer values, then the default value for this parameter will be 0.

Return value

This function does not return any value.

Example

Let us have a look at the code now.

#include <iostream>
#include <vector>
using namespace std;
void print_vector(vector<int> vec){
for(int x: vec)
cout << x << " ";
}
int main()
{
vector<int> vec;
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
vec.push_back(6);
vec.push_back(2);
vec.resize(3);
print_vector(vec);
cout << endl;
vec.resize(5, 10);
print_vector(vec);
cout << endl;
vec.resize(8);
print_vector(vec);
return 0;
}

Explanation

  • In line 1, we include the C++ standard header file for input output stream (iostream), which is used to read and write from streams.

  • In line 2, we include the header file for the C++ standard vector, which includes all the functions and operations related to the vector container.

  • In line 3, we use the standard (std) namespace, which means we use all the things within the std namespace.

  • From lines 5 to 8, we create a simple function that will accept a vector and print its elements.

  • In line 12, we declare the vector that contains integer type elements and name it vec.

  • From line 13 to 17, we push back the different elements to the vector container vec.

  • In line 19, we use the resize() function to change the vector size to 33. Here, we shrink the size from 55 to 33, and the last two elements are deleted. We can see the current elements in the vector in line 20.

  • In line 23, we use the resize() function to change the vector size from 33 to 55 and pass the value 1010 to initialize the new elements. Hence, the size is increased to 55 and two new elements are assigned with a value of 1010. We can see the current elements in the vector in line 24.

  • In line 27, we increase the size of the vector from 55 to 88 and don’t pass a value. 00 will be assigned to new elements by default. We can see the current elements in the vector in line 28.

So, in this way, we can use the vector::resize() method to increase or decrease the size of any vector container in C++.

Free Resources