I'm trying to make a program that uses a two-dimensional matrix. The problem is that to avoid to have limit, I don't know how to declare the matrix because the dimensions are not constant. I saw to how to realize vectors with not-constant dimension, creating objects. But for matrices nothing... How can I resolve?
-
4You could use a [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) – NathanOliver Sep 14 '15 at 13:22
1 Answers
If you know how to handle variable dimensioned vectors you can already solve your problem:
E.g. you can store a vector of column vectors of the m x n matrix A = (a_1, .., a_n), or a vector of row vectors A^t = (a_1, .., a_m).
Or you can sequentially store the m x n elements of such a matrix into a single vector. You would only need some enumeration of the elements to transform the indices i and j of element a_ij into a single index k.
E.g. enumerating row wise:
- k = f(i,j) = [m (i-1) + (j-1)] + 1 = m (i-1) + j
if i from {1,..,m}, j from {1,.., n} and k from {1, .., mn}.
The inverse, calculating i and j from k is:
i = g_1(k) = ((k-1) div m) + 1
j = g_2(k) = k - m (i-1) = k - m (g_1(k)-1)
If you do not care how this is done in principle but just need some working code, you should search for some C++ matrix library.

- 5,075
- 1
- 28
- 34