How can i add functionality to initialize this 2dvector class:-
template <typename T>
class uvector2d
{
public:
uvector2d(size_t xAxis=0, size_t yAxis=0, T const &
t=T()) : xAxis(xAxis), yAxis(yAxis), data(xAxis*yAxis, t)
{}
T & operator()(size_t xNum, size_t yNum)
{return data[xNum*yAxis+yNum];}
T const & operator()(size_t xNum, size_t yNum)
const {return data[xNum*yAxis+yNum];}
private:
size_t xAxis,yAxis;
uvector<T> data;
};
using uniform initialization as shown below:-
vector<vector<int> > vect{ { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
i am current using something like: uvector2dvect(3,3); vect(1,1)=10864;
You should implement a constructor that accepts initializer list of initializer lists as a parameter:
Within this constructor, you just need to copy list elements into one-dimensional
data
member vector.Note that this will work only if inner lists are of the same size (which is the only case that makes sense). Also, your storage of elements in 2D vectors seems to be column-major, so I suppose the initializer represents a list of columns. If it is actually a list of rows, then you need to transpose:
I have no option to try this code with
uvector
, so please report any problems if they happen to appear.