A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_vector_resize.htm below:

C++ vector::resize() Function

C++ vector::resize() Function

The C++ vector::resize() function is used to resize the container by a specified number of elements (n). The content is limited to the first n elements of the vector if n is less than the current vector size. The time complexity of the resize() function is linear.

If n is greater than the size of the current vector the new elements are added to the end of the vector. New elements are initialized with val if val is specified; otherwise, they are value-initialized.

Syntax

Following is the syntax for C++ vector::resize() Function −

void resize (size_type n);void resize (size_type n, const value_type& val);
Parameters Example 1

Let's consider the following example, where we are going to use resize() function.

#include <iostream>
#include <vector>
using namespace std;

int main(void) {
   vector<int> v;
   cout << "Initial vector size = " << v.size() << endl;
   v.resize(5, 10);
   cout << "Vector size after resize = " << v.size() << endl;
   cout << "Vector contains following elements" << endl;
   for (int i = 0; i < v.size(); ++i)
      cout << v[i] << endl;
   return 0;
}
Output

When we compile and run the above program, this will produce the following result −

Initial vector size = 0
Vector size after resize = 5
Vector contains following elements
10
10
10
10
10
Example 2

Considering the another scenario, where we are going to take an string type and decreasing the size of the vector.

#include <iostream>
#include <vector>
using namespace std;

int main(){
   vector<string> car = {"AUDI", "BUCATI", "CHERON", "BMW","BENZ"};
   cout << "current size of the vector: " << car.size() << endl;
   car.push_back("RS7");
   car.push_back("Q5");
   car.push_back("LAMBO");
   cout << "current size of the vector after insertion: " << car.size() << endl;
   car.resize(3);
   cout << "current size of the vector after resize: " << car.size() << endl;
   return 0;
}
Output

On running the above program, it will produce the following result −

current size of the vector: 5
current size of the vector after insertion: 8
current size of the vector after resize: 3
Example 3

In the following example, we are going to take n greater than the size of the current vector.

#include<iostream>  
#include<vector>  
using namespace std;
  
int main(){
   vector<string>  bike{"RX100","ACCESS","ACTIVA"};  
   cout<<"Elements before resize:"<<endl;  
   for(int i=0;i<bike.size();i++)  
      cout<<bike[i]<<" "<<endl;  
   bike.resize(5,"VESPA");  
   cout<<"Elements after resize:"<<endl;  
   for(int i=0;i<bike.size();i++)  
      cout<<bike[i]<<" "<<endl;  
   return 0;
}  
Output

When we execute the above program, it will produce the following result −

Elements before resize:
RX100 
ACCESS 
ACTIVA 
Elements after resize:
RX100 
ACCESS 
ACTIVA 
VESPA 
VESPA 
Example 4

Following is the example, where we are going to take float type and resize the vector with the values.

#include <iostream>
#include <vector>
using namespace std;

int main(){
   vector<float> tutorial = {1.2,2.3,0.4,0.5};
   cout << "Vector values before resize:\n";
   for(int i = 0; i < tutorial.size(); ++i)
      cout << tutorial[i] << " ";
   cout << "\n";
   tutorial.resize(6,0.67);
   cout << "Vector values after resize:\n";
   for(int i = 0; i < tutorial.size(); ++i)
      cout << tutorial[i] << " ";
   cout << "\n";
   return 0;
}
Output

On running the above program, it will produce the following result −

Vector values before resize:
1.2 2.3 0.4 0.5 
Vector values after resize:
1.2 2.3 0.4 0.5 0.67 0.67 

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4