Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container.
vector::resize()
The function alters the container’s content in actual by inserting or deleting the elements from it. It happens so,
- If the given value of n is less than the size at present then extra elements are demolished.
- If n is more than current size of container then upcoming elements are appended at the end of the vector.
Syntax:
vectorname.resize(int n, int val)
Below programs illustrate the working of the function
1.Size of the vector container is lowered.
CPP
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
cout << "Contents of vector before resizing:"
<< endl;
for ( int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << endl;
vec.resize(4);
cout << "Contents of vector after resizing:"
<< endl;
for ( int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
return 0;
}
|
Output:
Contents of the vector before resizing:
1 2 3 4 5
Contents of the vector after resizing:
1 2 3 4
2. Size of the vector container is increased.
CPP
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
cout << "Contents of vector before resizing:"
<< endl;
for ( int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << endl;
vec.resize(8);
cout << "Contents of vector after resizing:"
<< endl;
for ( int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
return 0;
}
|
Output:
Contents of the vector before resizing:
1 2 3 4 5
Contents of the vector after resizing:
1 2 3 4 5 0 0 0
3.Size of the vector container is increased and new elements are initialized with specified value.
CPP
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vec.push_back(4);
vec.push_back(5);
cout << "Contents of vector before resizing:"
<< endl;
for ( int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << endl;
vec.resize(12, 9);
cout << "Contents of vector after resizing:"
<< endl;
for ( int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
return 0;
}
|
Output:
Contents of the vector before resizing:
1 2 3 4 5
Contents of the vector after resizing:
1 2 3 4 5 9 9 9 9 9 9 9
How to resize in case of 2D Vectors (Vector of Vector)?
Approach / Steps followed – Variety 1:
- We can first resize the outer container vector<vector<int>>
- By doing that, we made it like { { }, { }, { } }
- Then we would initialize the internal container.
vector.resize(n, value);
C++
#include <bits/stdc++.h>
using namespace std;
void displayMatrix(vector<vector< int >> v){
int N = v.size();
int M = v[0].size();
for ( int i=0; i<N; i++){
for ( int j=0; j<M; j++){
cout<<v[i][j]<< " " ;
}
cout<< "\n" ;
}
}
int main()
{
vector<vector< int >> v;
cout<< "N: " <<v.size()<< "\n" ;
v.resize(4, vector< int > (4, -1));
cout<< "N: " <<v.size()<< " M: " <<v[0].size()<< "\n" ;
displayMatrix(v);
return 0;
}
|
Output
N: 0
N: 4 M: 4
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
Another Approach – Variety-2:
C++
#include <bits/stdc++.h>
using namespace std;
void displayMatrix(vector<vector< int > > v)
{
int N = v.size();
int M = v[0].size();
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
cout << v[i][j] << " " ;
}
cout << "\n" ;
}
}
int main()
{
vector<vector< int > > v;
cout << "N: " << v.size() << "\n" ;
v.resize(4);
for ( int i = 0; i < 4; i++)
v[i].resize(4, -1);
cout << "N: " << v.size() << " M: " << v[0].size()
<< "\n" ;
displayMatrix(v);
return 0;
}
|
Output
N: 0
N: 4 M: 4
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
The above 2 Approaches in this article is contributed by Balakrishnan R. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Dec, 2022
Like Article
Save Article