Thread: STL vector question

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    Question STL vector question

    i want to create a 2D matrix using vector. why the below is wrong?

    typedef std:vector<int> x
    typedef std:vector<x> y


    y matrix(7, x somevar(7) ) // trying to create 7 x 7 matrix

    look the second index ... x some_var(7) , i have written explicitly to crete a vector of 7 elements using typedef and then repalcing that into the matrix as the second argument.

    is it valid ?

    if not why it is not valid and what i should i do ?

    thanks
    blue_gene

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    The whole point of having a vector is so that you can create an array without worrying about the size, it just grows as you push new things into it.
    Besides, I don't think the constructor takes those arguments. Have you tried it? You should try doing a dummy program just to test some scenario and if you have problems, post.
    And remeber, use code tags when posting code.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Code:
    y matrix(7,x(7));
    //or
    x somevar(7);
    y matrix(7, somevar);
    However, the vector of vectors solution is really only usefull if some rows may be longer than others. In most cases I prefer to roll my own matrix class that overloads () to wrap my index calculations.
    Code:
    template<class Scalar=int>
    class matrix {
        size_t col;
        std::vector<Scalar> v;
    public:
        matrix(size_t rrow=0,size_t ccol=0) : col(ccol), v(row*col) {}
        Scalar & operator(size_t x, size_t y) { 
            return v[y*col+x];
        }
    };
    adding push_row, push_col taking refs to vectors and so forth as needed. This also adds a nice place to switch from row-major to col-major. The main thing is that once you get down to the assembly level all 2d arrays are really just long blocks of contiguous memory addressed with some multiplication as well. The matrix[x][y] syntax, where matrix is a vector of vectors, is deceptively simple, you are actually asking for a good bit more work than a multiply and add.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 05:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM