我想在一些代码中初始化一个 boost::multi_array 内联。但我不认为 boost::multi_array 支持从初始化列表进行初始化。这是我到目前为止所拥有的:
// First create a primitive array, which can be directly initialized
uint8_t field_primitive[4][8] = {
{ 1,1,1,1,1,1,1,1 },
{ 1,2,1,2,1,2,1,2 },
{ 1,1,2,2,2,2,2,2 },
{ 1,2,2,2,2,2,2,2 }
};
// Create the boost::multi_array I actually want to use
boost::multi_array<uint8_t, 2> field(boost::extents[4][8]);
// Compact but yucky approach to copying the primitive array contents into the multi_array.
memcpy(field.data(), field_primitive, field.num_elements() * sizeof(uint8_t));
我喜欢我可以使用花括号初始化器列表紧凑地表达矩阵内容。但我不喜欢“memcpy”,也不喜欢使用一次性原始数组。有没有更好的方法从代码中可读的内联值集填充我的 boost::multi_array ?