MASTERY26

Creation and use of matrixes in C++
Note that also you can use boost.ublas for matrix creation and manipulation and also boost.graph to represent and manipulate graphs in a number of ways, as well as using algorithms on them, etc.

Edit: Anyway, doing a range-check version of a vector for your purposes is not a hard thing:

template
class BoundsMatrix
{
std::vector inner;
unsigned int dimx
, dimy_;

public:
BoundsMatrix (unsigned int dimx, unsigned int dimy)
: dimx (dimx), dimy (dimy)
{
inner.resize (dimx*dimy_);
}

    T& operator()(unsigned int x, unsigned int y)
    {
            if (x >= dimx_ || y>= dimy_)
                    throw 0; // ouch
            return inner_[dimx_*y + x];
    }

};
Note that you would also need to add the const version of the operators, and/or iterators, and the strange use of exceptions, but you get the idea.

LINK:
http://stackoverflow.com/questions/618511/a-proper-way-to-create-a-matrix-in-c

TC1017

CC BY 4.0 MASTERY26 by ivanna lases is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.