gpt4 book ai didi

c++ - 将 Boost MultiArray (2d) 转换为普通二维数组的最简单方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 12:03:26 24 4
gpt4 key购买 nike

除了预分配和遍历所有元素之外,是否有将 2D Boost MultiArray 转换为普通 2D 数组的最佳/最简单方法?

#include "boost/multi_array.hpp"
#include <cassert>

int main ()
{
// Create 2D multi-array
typedef boost::multi_array<double, 2> array_type;
typedef array_type::index index;
array_type A(boost::extents[3][4]);

// Fill in some values ...
double value = 1.0;
for(index i = 0; i != 3; ++i)
for(index j = 0; j != 4; ++j)
A[i][j] = value;

// Convert to a double[3][4] ...
double **convert = ???

return 0;
}

最佳答案

1) 参见关于成员函数 data() 的文档 Boost MultiArray:

element* data();
const element* data() const;

This returns a pointer to the beginning of the contiguous block that contains the array's data. If all dimensions of the array are 0-indexed and stored in ascending order, this is equivalent to origin(). Note that const_multi_array_ref only provides the const version of this function.

或者

2) 你可以使用 multi_array_ref:

multi_array_ref is a multi-dimensional container adaptor. It provides the MultiArray interface over any contiguous block of elements. multi_array_ref exports the same interface as multi_array, with the exception of the constructors.

但无论如何 - 我认为将数据从 multi_array 复制到 C 数组是个坏主意。如果原因是遗留代码,请参阅 - http://www.boost.org/doc/libs/1_51_0/libs/multi_array/doc/user.html#sec_storage

关于c++ - 将 Boost MultiArray (2d) 转换为普通二维数组的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13143588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com