Search code examples
c++boostmultidimensional-arrayboost-multi-array

how to get specific elements from a const boost::multi_array


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.


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.