c++vector2d-vector

C++ 2D Vector Declaration


I am doing a homework and I see this vector declaration. I tried to find on Gooogle but still could not understand it. Here it is:

     vector<vector<int>>res = vector<vector<int>>(n,vector<int>(n,0));

I know vector<vector<int>> means 2D vector but I don't understand the part after this = vector<vector<int>>(n,vector<int>(n,0));


Solution

  • You can write like this too:

    vector<vector<int>>res(n,vector<int>(n,0));
    

    It means you have n vector<int> where each of them has n elements initialized to 0.