Question

I would like to know how to read elements from a const boost::multi_array object. Indeed to my knowledge I can't use the operator [] because it's also used to assignement.

I have a 3-D dimentional array. So how does one get the element myArray[i][j][k] when myArray is const.

Thanks in advance.

Was it helpful?

Solution

As an alternative to juanchopanza's answer you can also access elements via an index array build from a boost::array.

typedef boost::multi_array<double,3>::index tIndex;
typedef boost::array<tIndex, 3> tIndexArray;

tIndexArray index = {{ 1,2,3 }};
const double x = myArray( index );

Would give you the element myArray[1][2][3]. In case you are writing dimension-independent code this notation might be more useful than explicitly using the [] operator.

OTHER TIPS

You can read them by value or by const reference. Assuming your array holds elements of type T:

T x = myArray[1][2][3];
const T& y = myArray[1][2][3];

If you want a pointer to an element of the multi_array, then the pointer needs to be const:

const T* y = &myArray[1][2][3];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top