FerryPie
FerryPie

Reputation: 13

What Does 'vector<vector<int>>& indices' mean in the code?

I just cannot understand What vector<vector<int>>& indices mean .... along with the next line which is vector<vector<int>> matrix(n, vector<int>(m, 0));.

class Solution {
public:
    int oddCells(int n, int m, vector<vector<int>>& indices) 
    {
        vector<vector<int>> matrix(n, vector<int>(m, 0));
        for(int i=0;i<indices.size();i++) {
            for(int j=0;j<m;j++) matrix[indices[i][0]][j]++;
            for(int j=0;j<n;j++) matrix[j][indices[i][1]]++;
        }
        int res=0;
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) res+=matrix[i][j]%2!=0;
        }
        return res;
    }
};

Upvotes: 0

Views: 4715

Answers (1)

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39370

vector<X> means "a vector of X", regardless of what X is.

In your case you have a vector<vector<int>>, so your X is vector<int>. We can read that as "a vector of (a vector of int)".

Additionally, the & at the end means that it's a reference to such type.

This is sometimes used to represent a two-dimensional array, but it's a rather bad implementation of such for various reasons. In this case, it's clearly used to store a two-dimensional matrix.

The next line declares a value of the same type as used in the argument, initializing it with n vectors of size m, each containing m zeroes.

Upvotes: 4

Related Questions