vector of vectors question

This is a discussion on vector of vectors question within the C++ Programming forums, part of the General Programming Boards category; I frequently use vectors instead of arrays if I don't know what size it'll be or if I want the ...

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    vector of vectors question

    I frequently use vectors instead of arrays if I don't know what size it'll be or if I want the user to enter in the size. My question though is what do people usually do when you want to have a two or more dimensional array that you don't know what the size will be until run time?

    I've been using vector<vector<blah>> but this is kind of annoying and I was wondering if there was a better/easier way.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>I've been using vector<vector<blah>> but this is kind of annoying and I was wondering if there was a better/easier way.
    Write a vector of vectors once and throw it in a matrix class, then use that class when you need a 2d vector :-)
    *Cela*

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    So obvious, why didn't I think of that?! Thanks!

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Actually, one more question about this. Would it be possible to access data directly using []'s? I know how to overload the index operators for something like this:
    Code:
    myContainer[x];
    But I'm not sure how if even possible to overload something to get this:
    Code:
    myContainer[x][y];
    Or am I stuck having to do something like this for my matrix:
    Code:
    myMatrix.returnValue(x,y);

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Would it be possible to access data directly using []'s?
    Consider two classes, a matrix class and an array class. Use the array class with an overloaded [] operator and use that as a return value for the overloaded matrix [] operator :-)
    Code:
    template<typename T>
    class matrix {
    public:
      class array {
      public:
        T& operator[](int index);
        const T& operator[](int index) const;
        ...
      };
    
      array operator[](int index);
      const array operator(int index) const;
      ...
    };
    Then you can access it like this
    Code:
    matrix<int> a(5,10);
    ...
    cout<< a[i][j] <<endl;
    *Cela*

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Nice! Thanks, thats a useful trick to know!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about Vectors
    By ataman in forum C++ Programming
    Replies: 4
    Last Post: 06-02-2008, 01:36 PM
  2. 2d vectors question
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 07:31 PM
  3. Real quick question on vectors, if I may
    By hpy_gilmore8 in forum C++ Programming
    Replies: 7
    Last Post: 05-13-2003, 10:01 AM
  4. question about stacks (and vectors too for that matter)
    By Silvercord in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2003, 12:26 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21