Multidimensional Arrays in C++
Multidimensional Arrays in C++
C++ does not have a native way of doing multidimensional arrays like some other languages do. There are several ways to workaround this though. Here are some ideas:
Nested Vector method
Using the std::vector
object, nest one vector within another like so:
Note: the above array has not been initialised with anything, nor does it have a predefined size at compile time as vectors can be allocated dynamically, therefore so can our makeshift 2D Array. It’s effectively a vector of vectors with type int.
Nested array (C++11
) method
C++11 has a new feature in the standard library: std::array
. Unfortunately it also does not support multidimenional arrays, but you can nest one within another.
This is a 3x3 array as we’ve specified the dimensions in the declaration. We’ve also intialised the values in the array. Only available with C++11.
Using the Template Numerical Toolkit (TNT)
Eventually, the workarounds above can get a bit tiresome, or difficult to read using higher dimensional arrays. In LSDTopoTools, we use a library called the Template Numerical Toolkit, which adds support for multidimensional arrays, without the awkward looking syntax used above. The ‘user-friendly’ documentation is a bit sparse, but the basic examples are here.
The typical way to declare and intialise a TNT array is like so:
It’s not mentioned in the brief tutorial, but you can also do it like this:
Note that assigning ArrayA to ArrayB only produces a shallow copy (a view of the other array). To get a deep copy, you have to do B = A.copy()
. Also, in the LSDTT code, we tend to use a namespace declaration to avoid typing TNT::Array...
all the time:
Other Methods
You could define your own template (good luck!) or use C-style arrays (ugh…).
Other Libraries
In the LSDTT code you might also see the Matrix Template Library (MTL). This is another matrix/array library with some more powerful functions for doing matrix algebra. There’s also the Eigen library, which can do a lot of things that the TNT can’t, but is a much bigger library.