The vector is a very useful class of C for creating the dynamic array. The size of the vector can be changed at any time to solve any programming problem. Many built-in functions exist in C for doing the different types of tasks in a vector container. The resize() function is one of them. It is used to change the size of the vector. The vector size can be increased or decreased by using this function. The uses of resize() function in C vector have been explained in this tutorial.

Syntax:

The resize() function can be used in multiple ways. Two syntaxes of this function are given below.

void resize (size_type n)

If the value of the n is smaller than the original size of the vector object, then the size of the vector will be decreased. If the value of n is greater than the original size of the vector, then the size of the vector will be increased. If the value of n is equal to the original size of the vector, then the vector size will remain unchanged.

void resize (size_type n, const value_type& value);

If the second argument is used in this function, then the argument’s value will be added at the end of the vector.

Both of the resize() function returns nothing.

Pre-requisite:

Before checking the examples of this tutorial, you have to check the g compiler is installed or not in the system. If you are using Visual Studio Code, then install the necessary extensions to compile the C source code to create the executable code. Here, the Visual Studio Code application has been used to compile and execute the C code. Different uses of this function have shown in the next part of this tutorial by using various examples.

Example-1: Decrease the size of the vector

Create a C file with the following code to check how to decrease the vector’s size by using resize() function. A vector of 4 string values has been declared in the code. Three new values have been inserted into the vector after printing the original size of the vector. The size of the vector has been printed again after the insertion. The resize() function has been used to reduce the size of the vector to 5. The size of the vector has been printed again after reducing the size.

//Include necessary libraries

#include

#include

using 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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image1-52.png" data-lazy- height="532" src="data:image/svg xml,” width=”968″>

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 necessary libraries

#include

#include

using namespace std;

int main()

{

        //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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image2-49.png" data-lazy- height="534" src="data:image/svg xml,” width=”970″>

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.

// resizing of the vector

#include

#include

using namespace std;

int main()

{

        //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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/image3-48.png" data-lazy- height="534" src="data:image/svg xml,” width=”967″>

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.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/channel-logo-150×150.jpg6100f07f05869.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.