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.
Array sorting template C++
In some languages, there is a useful method that will sort the contents of an array based on the order of another:
For example, C# has this:
This doesn’t quite translate easily into C++. But don’t worry, you can use this handy template:
Floats and Ints in LSDRaster
I have been having trouble with bil files if I import ASTER data on my windows machine. It seemed to be limited to windows but I now think the problem could be systematic.
I think I have fixed it but you’ll need to check by rerunning some test analyses and making sure the behaviour is what you expect.
ENVI files have a ‘DATATYPE’ entry in the hdr file that give the number of bytes in each data point. ASTER data is 2 bytes. It turns out this is a ‘short int’ type. It can only go up to ~32000.
So 1) I have changed the read raster functions to read short ints now 2) I have changed the LSDIndexRaster to go back to printing normal 4 bit ints so that we don’t run out of numbers…I think this has happened to big DEMs where we need the NodeIndex.
Sample Post
This is a sample post.
Below is just about everything you’ll need to style in the theme. Check the source code to see the many embedded elements within paragraphs.
Heading
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
Body text
This is strong.
This is figure
This is emphasized.
53 = 125. Water is H2O.
The New York Times (That’s a citation).
Underline.
HTML and CSS are our tools.
Blockquotes
Justification: This species is listed as Extinct in the Wild, as all populations are still under captive management. The captive population in China has increased in recent years, and the possibility remains that free-ranging populations can be established some time in the near future. When that happens, its Red List status will need to be reassessed.
I follow up the quest. Despite of day and night and death and hell.
-- Idylls of the King, Tennyson
List Types
Ordered Lists
- Item one
- sub item one
- sub item two
- sub item three
- Item two
Unordered Lists
- Item one
- Item two
- Item three
Tables
Header1 | Header2 | Header3 |
---|---|---|
cell1 | cell2 | cell3 |
cell4 | cell5 | cell6 |
cell7 | cell8 | cell9 |
Kingdom | Phylum | Class | Order | Family |
---|---|---|---|---|
ANIMALIA | CHORDATA | MAMMALIA | CETARTIODACTYLA | CERVIDAE |
Code Snippets
Syntax highlighting via Pygments
highlight with line number.
1
2
3
def foo
puts 'foo'
end
you can use latex with double $$
<q> tag
here is a <q> q tag </q>
here is a q tag