Thread: Building matrix by using vectors?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    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,

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Say
    vector< vector< int > >
    ?

  3. #3
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    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';
      }
    }

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    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?

  5. #5
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >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!

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    Thank you for the help Slacker

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM