EDITED
For some reason when I run this on Repl.it, I get an out_of_range error
All I am trying to do is iterate through a 2d vector vertically, and do some other implementations.
This is the code:
#include <iostream>
#include <vector>
using namespace std;
void matrixElementsSum(vector<vector<int>> matrix) {
int sum = 0;
int i = 0;
for (int j = 0; j < matrix.at(j).size(); j++)
{
for(i = 0; i<matrix.size(); i++)
{
cout << matrix.at(i).at(j) << " ";
}
cout << endl;
}
}
int main()
{
vector<vector<int>> vect
{
{1, 1, 1, 0},
{0, 5, 0, 1},
{2, 1, 3, 10}
};
cout << matrixElementsSum(vect);
return 0;
}
And output is
1 0 2
1 5 1
1 0 3
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)
Normally, this error means I am trying to read past the vector size but it is not the case here(at least, that is what I think)
It is hard understanding what is wrong here. I would appreciate any kind of help
Note: I don't want to iterate horizontally
question from:
https://stackoverflow.com/questions/65661552/getting-an-out-of-range-error-iterating-through-a-2d-vector-in-c 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…