c++vectorstlassign2d-vector

How to assign size to a globally declared 2d vector in the main function in C++?


#include <bits/stdc++.h>
using namespace std;
vector< vector<int> >Adj;
int main()
{
    int n;
    cin>>n;
    Adj.assign(n);
}

This code doesn't work for assigning size to the vector.


Solution

    1. Use resize method 'Adj.resize(n)'. The function alters the container’s content in actual by inserting or deleting the elements from it.
    2. If the given value of n is less than the size at present then extra elements are demolished.
    3. If n is more than current size of container then upcoming elements are appended at the end of the vector