std ::大小未知的数组作为类成员

用户81993

我正在制作N维图像格式,我想将原始数据存储在

std::array<T, multiple of all dimensions>* where T is the type of single channel pixel value

我希望构造函数采用诸如{20,30,50}之类的通道数组来制作20x30x50位图,例如,这将使数据的总长度为30000。最后,我希望能够像这样声明通道

auto* channelRed = new Channel<uint32_t>({20, 30, 50});

问题是std :: array希望在模板参数中传递大小,这会在我的N维计划中造成麻烦。

如何在类中将std :: array指针设置为类字段,以便可以在构造函数执行期间定义数组的长度?

PS!是的,我知道我可以轻松地使用常规数组,这就是我现在正在做的事情。我只是想弄清楚std :: array有什么用。

巴里

你不能 一个std::array必须知道它在编译时的大小。这是该类型的一部分!Astd::array<int, 2>和astd::array<int, 3>不仅大小不同,而且是完全不同的类型。

您需要的是动态大小的数组,而不是静态大小的数组std::vector<uint32_t>

template <typename T>
class Channel {
    std::vector<T> v;

public:
    Channel(std::initializer_list<T> dims)
    : v(std::accumulate(dims.begin(), dims.end(), size_t{1}, 
                        std::multiplies<size_t>{}))
    { }
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章