多维数组元素的分配-是否需要左值?

要存储二维以上的图像,请使用具有数据成员的类

std::vector <size_t> sizes;    // dimensions of the images
std::vector <size_t> strides;  // strides to walk through the data
std::vector <double> *data;    // data points stored as 1-dimensional

并获取和设置各个点的值,我具有这些功能

// operator[] for positional addressing
double const* operator[] ( std::initializer_list < size_t > const& indices ) const {
   size_t const offset =
          std::inner_product ( indices.begin(),indices.end(),strides.begin(),0 );
   return & ( (*data) [ offset ] );
}
double* operator[] ( std::initializer_list < size_t > const& indices ) {
   size_t const offset =
          std::inner_product ( indices.begin(),0 );
   return & ( (*data) [ offset ] );
}

我可以使用这些功能在指定位置获取数据并打印,例如,使用std::cout << my_image[{1,2,3}]打印3D图像,例如 eg ,但是当我要设置它们时, eg 使用my_image[{1,3}] = 10.;,然后出现错误lvalue required as left operand as assignment。我遵循ean earlier post的建议,传递了[]运算符(按值本身),一个const和一个非const的指针。应该也不能使用这种语法设置图像元素吗?

kingkong1900 回答:多维数组元素的分配-是否需要左值?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3165260.html

大家都在问