#include <string>
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
class matrix
{
public:
matrix(int);
void func(int);
vector<vector<double> > m;
};
matrix::matrix(int size)
{
//set 3 X 3 = 9 elements to zero
vector<vector<int> > m(size, vector<int>(size));
// success
cout << "test1 if m[0][0] equals to zero: " << m[0][0] << endl;
}
void matrix::func(int size)
{
// failure, can't pass till here
cout << "test2 if m[0][0] equals to zero: " << m[0][0] << endl;
for(int i = 1; i != size; ++i)
{
m[i][i] = 0;
}
}
int main()
{
int input1 = 3;
matrix mat(input1);
mat.func(input1);
}
I want to create a 2D dimensional vector rather using array. However, I got error. I create vector<vector<int> > m(size, vector<int>(size)) to make sure all elements in vector are equal to zero, and I passed the test1. I can't pass test2 which I commented "failure".
Aucun commentaire:
Enregistrer un commentaire