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.
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.
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 <iostream>
#include <vector>
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.
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 <iostream>
#include <vector>
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.
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 <iostream>
#include <vector>
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.
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.
from Linux Hint https://ift.tt/3BQQE0S
0 Comments