I want to do a function that given 2 matrix returns the sum of both.I think the problem is in how I initialize the Matrix ‘t’.
#include <iostream>
#include <vector>
using namespace std;
typedef vector< vector<int> > Matrix;
Matrix sum(const Matrix&a,const Matrix&b){
Matrix t;
for(int i=0;i<a.size();i++)
for(int j=0;j<a.size();j++)
t[i][j] = a[i][j] + b[i][j];
return t;
}
You’ll need to initialize the rows and columns of
twith something like:That will make a
row_countbycol_countmatrix filled with zeroes.On a side note about performance: comparing to
.size()in a for loop means that before each iteration,.size()has to be calculated again. You can save a bit of processing (which adds up for massive data sets) by pre-calculating it like so: