-1

I'm not understanding role of vector here and how the adj[0] is storing data{1,2}.

int main()
{
    vector <int>* adj;
    cout<<adj<<endl;

    adj = new vector <int> [10];  //did'nt understood what this line is doing

    adj[0].push_back(1);
    adj[0].push_back(2);

    for(int i=0; i<2; ++i){
        cout<<adj[0][i]<<endl;    //how adj[0][0] is storing data
    }

    return 0;
}
Venkatesh
  • 11
  • 1

1 Answers1

0

Comments are inlined

int main()
{
    vector <int>* adj;
    cout<<adj<<endl;

    adj = new vector <int> [10];  //This will allocate an array of length 10 whose each element is a vector. Interpret it as a 2D array

    adj[0].push_back(1); // Here adj[0][0] is storing data
    adj[0].push_back(2); // Here adj[0][1] is storing data

    for(int i=0; i<2; ++i){
        cout<<adj[0][i]<<endl;   
    }

    return 0;
}

Make sure to delete the dynamically allocated array with delete [] when your work is done.

The Philomath
  • 954
  • 9
  • 15