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:

#include <vector>

std::vector< std::vector<int> > MyArray;

// You can index this with normal array syntax:
MyArray[i][j] = x;

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.

#include <array>

std::array< std::array<float, 3>, 3 > MyArray  = { { {5, 8, 2}, {8, 3, 1}, {5, 3, 9} } };

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:

TNT::Array2D <float> mynewarray;  // Empty 2d array
TNT::Array2D <float> myotherarray(4,5);  // 4x5 array
TNT::Array2D <float> mythirdarray(3,3, 0.0); // Initialise zeros.

It’s not mentioned in the brief tutorial, but you can also do it like this:

MyArray = TNT::Array2D <float> (3,3);

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:

#include "TNT/tnt.h"
using namespace TNT;

Array2D <int> somearray(3,3);

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.


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:

Array.sort(array, array)

This doesn’t quite translate easily into C++. But don’t worry, you can use this handy template:

template <class T1, class T2, class Pred = std::less <class T> >
struct sort_pair_second
{
  bool operator()(const std::pair<T1,T2>&left, constd std::pair<T1,T2>&right)
  {
    Pred p;
    return p(left.second, right.second);
  }
};

// Usage:

std::sort(v.begin(),v.end(), sort_pair_second <int,int>() );

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.


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

Elaphurus davidianus

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

  1. Item one
    1. sub item one
    2. sub item two
    3. sub item three
  2. 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

#container {
  float: left;
  margin: 0 -240px 0 0;
  width: 100%;
}

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