Is there a more natural way to represent the matrix T than the vector <vector <T>>?
Context:
I'm trying to learn C ++ (while reading some stackoverflow files recommended for publication), and I decided to try to make a cellular machine program with basic functionality just for the sake of learning (and because I'm interested).
Question:
Is there a more natural way of representing a matrix of cell elements than using the vector <vector <Cell β? I am looking for potential alternatives in standard libraries or in some other popular libraries. A commentary on effectiveness will be appreciated.
I had no problems using vector vectors or problems with syntax, I just wanted to know the alternatives. And since, since I am inexperienced, every time I write some kind of code, I think that there are probably much simpler ways to do this that I wonβt find myself.
This is my first question, so if I did something against the recommendations for the questions, I would really appreciate you pointing this out.
A useful related question for future reference: Is a vector <vector <double β> a good way to make a matrix class?
Matrix<double, 13, 3> From the Eigen3 library http://eigen.tuxfamily.org/dox/group__QuickRefPage.html Eigen3 provides almost any operation you need for linear algebra, and is well tested and used by a wide user base.
There are usually two ways to represent a two-dimensional array.
An array of pointers to other arrays is used, which is the
std::vector<std::vector<T>>model.One tightly packed array containing one line after another in memory. Then the element
i, jcan be found ini + j * width.
The built-in multidimensional arrays in C ++ are a little strange, since they have a similar syntax to 1, but type T[10][10] with compilers I know implements a type 2 scheme. I donβt know, from my point of view, if this provided by the standard.
I would have a desire to do something like that. I donβt know how many speed advantages it can have over a vector of vectors, so I would check some tests to verify this. I guess he will have some speed advantage.
typedef int Cell; int main() { // manage the raw multidimensional array pointer in a smart pointer // so no need to worry about memory leaks std::unique_ptr<Cell[][100]> cell_uptr(new Cell[100][100]); // work from the raw pointer (maybe save a dereference?) Cell(*cells)[100] = cell_uptr.get(); for(size_t x = 0; x < 100; ++x) { for(size_t y = 0; y < 100; ++y) { // the compiler should be able to access cells[x][y] // based on a simple calculation rather than a second dereference cells[x][y] = 0; } } // no need to worry about cleaning up } NOTE. The disadvantage of this approach is that the size of the array is fixed at compile time.