Topic: multidemensional array using stl vector


Author: Peter Kelm <kelm@lise.physik.TU-Berlin.DE>
Date: 1997/06/30
Raw View
hi,

I want to create a two-dimensional array using stl vector
size is determined at runtime. But how do I do this?

#include <vector>

typedef vector<double> row;
typedef vector<row*>   matrix;
matrix myMatrix;

the code above works but if I do it like this i cannot use
the "common" subscript operator

myMatrix[1][2] = 5.4;

Ideas for a simple solution?

Thanks                Peter

         --- beam me up Scotty, this planet sucks ---
                                                        Plan B
Peter Kelm (kelm@physik.tu-berlin.de)

Technical University Berlin
Physics Department
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: ouchida@sirius.com (Wayne B. Ouchida)
Date: 1997/07/03
Raw View
I wouldn't use a STL vector to implement a two-dimensional whose size
is determined at runtime. I would implement this using a technique
utilizing a proxy class as implemented in the following example:

#include <iostream>
using namespace std;

template <class T>
class Matrix {
public:
    Matrix(int i, int j) : m(new T[i * j]), dim1(i), dim2(j) { }
    ~Matrix() { delete [] m; }

    class Proxy {
    public:
        T& operator[](int j) { return *(p + j); }
        T* p;
    };

    Proxy operator[](int i) {
        Proxy temp;
        temp.p = m + i * dim2;
        return temp;
    }
private:
    T* m;
    unsigned int dim1;
    unsigned int dim2;
};

main()
{
    int dim1 = 3;
    int dim2 = 5;
    Matrix<int> a(dim1, dim2);     // Allocate a 2 dimensional
                                   //   array at run time

                                   // Initializes an array
    int i, j;                      //  11 12 13 14 15
    for (i = 0; i < dim1; ++i)     //  21 21 23 24 25
        for (j = 0; j < dim2; ++j) //  31 32 33 34 35
            a[i][j] = (i+1)*10 + (j+1);

    for (i = 0; i < dim1; ++i) {   // Print out the array
        for (j = 0; j < dim2; ++j)
            cout << a[i][j] << " ";
        cout << endl;
    }

    return 0;
}

Note that I intentional left out "const" operators in the above class
definition for simplicity and clarity.

The above implementation can be explained by noting that a
two-dimensional array of the form a[i][j] is in reality a
one-dimensional array, i.e., (a[i])[j].

As a concrete example, consider a[2][4]. This is equivalent to
(a[2])[4]. '[2]' of a[2][4] invokes the overloaded Matrix class
operator[]. This operator returns a Proxy class containing a pointer
to the beginning of the appropriate row of the two-dimensional array.
Next the right-most operator[], i.e., '[4]', is invoked on this return
Proxy class. This operator is the overloaded Proxy class's operator[].
This operator returns a reference to the appropriate element of the
row. I hope this helps.

 -- Wayne


On 30 Jun 97 19:32:31 GMT, Peter Kelm <kelm@lise.physik.TU-Berlin.DE>
wrote:

>hi,
>
>I want to create a two-dimensional array using stl vector
>size is determined at runtime. But how do I do this?
>
>#include <vector>
>
>typedef vector<double> row;
>typedef vector<row*>   matrix;
>matrix myMatrix;
>
>the code above works but if I do it like this i cannot use
>the "common" subscript operator
>
>myMatrix[1][2] = 5.4;
>
>Ideas for a simple solution?
>
>Thanks                Peter
>
>         --- beam me up Scotty, this planet sucks ---
>                                                        Plan B
>Peter Kelm (kelm@physik.tu-berlin.de)
>
>Technical University Berlin
>Physics Department
>---
>[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
>[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
>[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
>[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
>[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]