Matrix class using vector

Hi, I want create a matrix class using the STL conteiners vector, but I have some problem when I want set a value of the matrix. Could anyone help me?

#include<iostream>
#include<vector>
using namespace std;


class Matrix {
private:
vector<vector<int> > Mat;
Matrix() {};
public:
Matrix(vector<vector<int> > mat) : Mat(mat) {}

Matrix(const Matrix& m)
{
cella = m.cella;
}

~Matrix() {}

int GetNum(int r, int c)const {return Mat[r][c];};
int GetRow()const {return (Mat.size());};
int GetColumn()const {return (Mat[0].size());};

void SetValue(int y, int x, int value)
{

vector<vector<int> > temp(GetRow(), vector<int>(GetColumn(),0));
Matrix Temp(temp);

Temp.Mat[y][x] = value;
}

void Print()
{
vector<vector<int> > temp(GetRow(), vector<int>(GetColumn(),0));
Matrix Temp(temp);

for(int i=0; i<Temp.GetRow(); i++)
{
for(int j=0; j<Temp.GetColumn(); j++)
{
cout<<Temp.Mat[i][j]<<"\t";
}
cout<<endl;
}
}

void Clear()
{
vector<vector<int> > temp(GetRow(), vector<int>(GetColumn(),0));
Matrix Temp(temp);

for(int i=0; i<Temp.GetRow(); i++)
{
for(int j=0; j<Temp.GetColumn(); j++)
{
Temp.Mat[i][j] = ' ';
}
}
}

};

int main()
{
vector<vector<int> > A(4,vector<int>(4,0));

Matrix m(A);
m.Print();
cout<<endl;
m.Clear();
m.SetValue(1,1,5);
m.Print();

}
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

The oddball below is like your matrix, except it stores an int and not vector of vectors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>

class Oddball {
private:
  int secret;
public:
  Oddball( int value ) : secret(value) {}
  Oddball(const Oddball& m) : secret(m.secret) {}
  ~Oddball() {}

  void SetValue(int value) {
    int temp = 0;
    Oddball WhoIsThis( temp );
    WhoIsThis.secret = value;  // Really ????
  }

  void Print() {
    int temp = 0;
    Oddball WhoIsThis( temp );
    std::cout << WhoIsThis.secret << '\n';
  }
};

int main()
{
  int A = 0;
  Oddball m(A);
  m.Print();
  std::cout << '\n';
  m.SetValue(5);
  m.Print();
}

What is the temporary WhoIsThis that is modified (and promptly forgotten) or shown?
It has nothing to do with the secret of the Oddball that we "set value" or "print" for.

Perhaps:
1
2
3
4
5
6
7
  void Oddball::SetValue(int value) {
    secret = value; // you can write this->secret if that feels better
  }

  void Oddball::Print() const {
    std::cout << secret << '\n';
  }
Last edited on
Your code with code tags:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<vector>
using namespace std;


class Matrix {
private:
	vector<vector<int> > Mat;
	Matrix() {};
public:
	Matrix(vector<vector<int> > mat) : Mat(mat) {}

	Matrix(const Matrix& m)
	{
		cella = m.cella;
	}

	~Matrix() {}

	int GetNum(int r, int c)const { return Mat[r][c]; };
	int GetRow()const { return (Mat.size()); };
	int GetColumn()const { return (Mat[0].size()); };

	void SetValue(int y, int x, int value)
	{

		vector<vector<int> > temp(GetRow(), vector<int>(GetColumn(), 0));
		Matrix Temp(temp);

		Temp.Mat[y][x] = value;
	}

	void Print()
	{
		vector<vector<int> > temp(GetRow(), vector<int>(GetColumn(), 0));
		Matrix Temp(temp);

		for (int i = 0; i<Temp.GetRow(); i++)
		{
			for (int j = 0; j<Temp.GetColumn(); j++)
			{
				cout << Temp.Mat[i][j] << "\t";
			}
			cout << endl;
		}
	}

	void Clear()
	{
		vector<vector<int> > temp(GetRow(), vector<int>(GetColumn(), 0));
		Matrix Temp(temp);

		for (int i = 0; i<Temp.GetRow(); i++)
		{
			for (int j = 0; j<Temp.GetColumn(); j++)
			{
				Temp.Mat[i][j] = ' ';
			}
		}
	}

};

int main()
{
	vector<vector<int> > A(4, vector<int>(4, 0));

	Matrix m(A);
	m.Print();
	cout << endl;
	m.Clear();
	m.SetValue(1, 1, 5);
	m.Print();

}


- Line 11: it would be better to change to Matrix(int rows, int columns) : Mat(vector<vector<int> >(rows, vector<int>(columns, 0))) {}.
So you can change lines 66-68 to the simplest Matrix m(4, 4);.

- Line 15: what is "cella"? Maybe you would write
1
2
3
4
	Matrix(const Matrix& m)
		: Mat(m.Mat)
	{
	}


- Lines 20-22: the functions should be named GetValue, GetRows and GetColumns

- Line 24: you should change it to void SetValue(int r, int c, int value)

- Lines 26-30: you are assigning the value to a temporary object. Why are you using a temporary object? Why not simply writing Mat[r][c] = value;?

- functions Print and Clear: see function SetValue, don't create a temporary object

- Line 57: Mat contains ints not chars. Why are you using ' ' instead of 32? And why not 0?

Topic archived. No new replies allowed.