The vector is a incredibly handy class of C++ for developing the dynamic array. The sizing of the vector can be adjusted at any time to resolve any programming difficulty. Lots of crafted-in functions exist in C++ for performing the distinctive kinds of jobs in a vector container. The resize() function is just one of them. It is made use of to alter the size of the vector. The vector dimensions can be enhanced or reduced by working with this operate. The makes use of of resize() perform in C++ vector have been discussed in this tutorial.

## Syntax:

The resize() functionality can be used in various techniques. Two syntaxes of this functionality are specified below.

void resize (dimension_type n)

If the benefit of the n is lesser than the first dimensions of the vector item, then the measurement of the vector will be reduced. If the price of n is higher than the initial size of the vector, then the dimensions of the vector will be amplified. If the value of n is equivalent to the original sizing of the vector, then the vector dimensions will keep on being unchanged.

void resize (dimensions_form n, const price_sort& value)

If the second argument is employed in this function, then the argument’s value will be additional at the finish of the vector.

Equally of the resize() purpose returns almost nothing.

## Pre-requisite:

Prior to checking the illustrations of this tutorial, you have to examine the g++ compiler is mounted or not in the procedure. If you are using Visual Studio Code, then set up the needed extensions to compile the C++ resource code to develop the executable code. Listed here, the Visual Studio Code software has been utilised to compile and execute the C++ code. Different makes use of of this function have revealed in the future section of this tutorial by employing a variety of examples.

## Case in point-1: Reduce the dimension of the vector

Develop a C++ file with the following code to verify how to lower the vector’s dimensions by using resize() purpose. A vector of 4 string values has been declared in the code. Three new values have been inserted into the vector after printing the authentic size of the vector. The measurement of the vector has been printed once more soon after the insertion. The resize() purpose has been utilized to cut down the dimension of the vector to 5. The size of the vector has been printed all over again right after minimizing the sizing.

#contain

#consist of

making use of namespace std

int main()

//Declare a vector of string values

vector<string> foods = “Cake”, “Pastry”, “Pizza”, “Burger”

cout << “The current size of the vector: “ << foods.size() << endl

//Add three elements

foods.push_back(“Pasta”)

foods.push_back(“French Fry”)

foods.push_back(“Chicken Fry”)

cout << “The current size of the vector after insertion: “ << foods.size() << endl

//Resize the vector

foods.resize(5)

cout << “The current size of the vector after resize: “ << foods.size() << endl

return 0

## Output:

The following output will appear after executing the above code. The output shows that the original size of the vector was 4, the size became 7 after inserting 3 new values, and the size became 5 after using the resize() function.

## Example-2: Increase the size of the vector

Create a C++ file with the following code to check how to increase the vector’s size by using resize() function. A vector of 5 integer numbers has been declared in the code. The size of the original vector has been printed before increasing the size of the vector using resize() function. The size of the vector has been printed again after resizing the size to 8. Next, 5 numbers have been inserted at the end of the vector, and the modified vector’s size has been printed again.

#include

#include things like

working with namespace std

int major()

//Declare a vector of integer values

vector<int> numbers = 10, 90, 20, 80, 30

cout << “The current size of the vector: “ << numbers.size() << endl

//Resize the vector

numbers.resize(8)

cout << “The current size of the vector after resize: “ << numbers.size() << endl

//Add 5 numbers into the vector

numbers.push_back(60)

numbers.push_back(40)

numbers.push_back(50)

numbers.push_back(70)

numbers.push_back(100)

cout << “The current size of the vector after insertion: “ << numbers.size() << endl

return 0

## Output:

The following output will appear after executing the above code. The output shows that the original size of the vector was 5, the size became 8 after resizing the vector, and the size became 13 after inserting 5 elements into the vector.

## Example-3: Resize the vector with the values

Create a C++ file with the following code to check how to resize the vector by inserting the same value multiple times. A vector of 5 float numbers has been declared in the code. The resize() function has been used to resize the vector size to 7 and insert the number 5.55 into the vector two times. The content of the original vector and the modified vector will be printed after executing the code.

#include

#include things like

using namespace std

int most important()

//Declare a vector of float values

vector<float> numbers = 7.89, 3.98, 5.56, 9.65, 2.33

cout << “The values of the vector before resize:n“

//Iterate the vector using loop to print the values

for(int i = 0 i < numbers.size() ++i)

cout << numbers[i] << ” “

cout << “n“

//Resize the vector with values

numbers.resize(7, 5.55)

cout << “The values of the vector after resize:n“

//Iterate the vector using loop to print the values

for(int i = 0 i < numbers.size() ++i)

cout << numbers[i] << ” “

cout << “n“

return 0

## Output:

The following output will appear after executing the above code. The output shows that the number 5.55 has been inserted two times at the end of the vector.

## Conclusion:

Using the resize() function to change the size of the vector with value or without value has been described in this tutorial by using simple examples. The new C++ coder will be able to change the size of the vector-based on the requirement by using resize() function after reading this tutorial.