1

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));

Si Dang
  • 25
  • 1
  • 6
  • When you write `vector>res = vector>(n,vector(n,0))`, the right side means that you are creating a vector of size `n` where each element is a vector of size `n` where all elements are 0. – Daniel Oct 04 '19 at 15:45
  • The first `n` after `=` declares `n` vectors of type `vector`. The second part, i.e. `vector(n, 0)` declares a vector of size `n` and fills all `n` elements with zeroes. Thus, the first part will declare `n` vectors that are of size `n` with all zeroes as elements. – oneturkmen Oct 04 '19 at 15:45
  • 1
    You don't really need this, you can write `vector> res(n,vector(n,0))`. – Daniel Oct 04 '19 at 15:45
  • `res` is a value of `vector>` type, '=' is assignment operator and `vector>(n,vector(n,0))` is object of a `vector>` class initialization with call to constructor. What is happening: (will read statement from right to left), temporary variable of type `vector>` created, then it assigned (copyed) to the `res` variable of the same type. – Victor Gubin Oct 04 '19 at 15:46
  • I got it, thank you all!! – Si Dang Oct 04 '19 at 15:49

1 Answers1

0

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.

Oblivion
  • 7,176
  • 2
  • 14
  • 33