简体   繁体   English

如何从const boost :: 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. 我想知道如何从const boost :: multi_array对象读取元素。 Indeed to my knowledge I can't use the operator [] because it's also used to assignement. 实际上,据我所知,我不能使用运算符[],因为它也用于赋值。

I have a 3-D dimentional array. 我有一个3D三维数组。 So how does one get the element myArray[i][j][k] when myArray is const. 因此,当myArray为const时,如何获取元素myArray [i] [j] [k]

Thanks in advance. 提前致谢。

As an alternative to juanchopanza's answer you can also access elements via an index array build from a boost::array. 作为juanchopanza答案的替代方法,您还可以通过从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] . 将为您提供元素myArray[1][2][3] In case you are writing dimension-independent code this notation might be more useful than explicitly using the [] operator. 如果您要编写与维度无关的代码,则此符号可能比显式使用[]运算符有用。

You can read them by value or by const reference. 您可以按值或const引用读取它们。 Assuming your array holds elements of type T: 假设您的数组包含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: 如果您想要一个指向multi_array元素的指针,则该指针需要为const:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM