I'm converting some functions from Matlab to C++, and there are something to do with matrix. I found this simple function somewhere on the Internet:
typedef std::vector<std::vector<double> > Matrix;
Matrix sum(const Matrix& a, const Matrix& b) {
size_t nrows = a.size();
size_t ncols = a[0].size();
Matrix c(nrows, std::vector<double>(ncols));
for (int i = 0; i < nrows; ++i) {
for (int j = 0; j < ncols; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
return c;
}
Can anyone explain me why they used const Matrix& a
as the input, instead of Matrix a
? Are they using it as a habit, or is there any benefit of using it since I did not see any difference between the results of 2 versions (const Matrix& a
and Matrix a
as input).