# Building matrix by using vectors?

• 12-21-2005
sectoid
Building matrix by using vectors?
Hey, first post.
I'm really more of a java-programmer but I'm starting out learning c++ more seriously now.
Was coding away and got stuck bad. I have built a vector class,
works the same way more or less like std vector. Anyway
I want to use it to build a 2d matrix.

The idea I had and tried out was like this:

in matrix.h (bit pseudo)

Code:

```class Matrix {  private:  int m, n;  vector<int> *m;  public:  matrix(int columns, int rows); } in matrix.cc  matrix(int columns, int rows) {   m = new vector<int>(rows)[columns]; }```
m = new vector<int>(rows)[columns];

this does not work..

I tried out with instead of having a vector<int> *m to have
vector<vector<int> > *m, and initialise in matrix.cc with
m = new vector<vector<int>(columns) >(rows)
but this did not work either.

I want the matrix dynamically stored

thanks,
• 12-21-2005
Salem
Say
vector< vector< int > >
?
• 12-21-2005
Slacker
The pointer makes it more awkward because you have to dereference it before doing anything else, but here's the process:
Code:

```#include <iostream> #include <vector> using std::vector; int main() {   // Declare a vector of vectors of int   vector<vector<int> > *m;   // Same pattern as the declaration   m = new vector<vector<int> > ( 5, vector<int> ( 10, 0 ) );   for ( int i = 0; i < 5; i++ ) {     for ( int j = 0; j < 10; j++ )       std::cout << (*m)[i][j] << ' ';     std::cout << '\n';   } }```
• 12-21-2005
sectoid
Slacker,

Yea that worked well thanks, problem is I got my own vector class that won't take take ( 5, vector<int> ( 10, 0 ) ); this as constructor arguments.

here is mine:
http://rafb.net/paste/results/CPKkOF55.html

Most of is not really relevant here but anyway,

any ideas on how to make a matrix out of this?
• 12-21-2005
Slacker
>any ideas on how to make a matrix out of this?
You don't. A default constructor is required to allocate memory for an array of some class. In this case, your default constructor is private, which breaks this line:
Code:

`u = new T[size];`
You can get it to work, but you'll want to modify vektor<> to grow dynamically so that a default constructor, and an empty vector, makes sense. You could do it by tossing flexibility and locking yourself into a two dimensional matrix like you would with dynamic arrays, assuming you uncover the boundary bugs in your insert method:
Code:

```int main() {   vektor<int> **m = new vektor<int>*[5];   for ( int i = 0; i < 5; i++ ) {     m[i] = new vektor<int> ( 10 );     for ( int j = 0; j < 10; j++ )       m[i]->insert ( j, 0 );   }   for ( int i = 0; i < 5; i++ ) {     for ( int j = 0; j < 10; j++ )       std::cout << (*m[i])[j] << ' ';     std::cout << '\n';   } }```
But in all honesty, you'd have an easier time writing a custom matrix class from scratch instead of trying to use the vector class you have.

Cheers!
• 12-21-2005
Darryl
m = new vector<int>(rows*columns);
instead of doing an array of vectors, just a flat allocation since the matrix size doesn't change once created.

to access a x,y location you'd use, x*columns+ y
• 12-21-2005
sectoid
Thank you for the help Slacker