Rserve Function of std::vector

Are these two Things the same???

  1. vector v(100);
    and
  2. vector v;
    v.reserve(100);

If these Two are not same then please share the diffrences

Hey, when you declare a vector as

vector<int> v(100);
vector constructor will be called and initialize the vector v with default size 100.
but when you write this

vector<int> v1;
 v1.reserve(100);

vector v1 will have a reserved space of 100 but its size will be 0 at that time, its size will depend upon how many elements are added to it.

you can test this using this code snippet:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
    vector<int> v(100);
    cout<<v.size()<<endl;
    vector<int> v1;
    v1.reserve(100);
    cout<<v1.size()<<endl;
}
1 Like

Thank you for the help