gpt4 book ai didi

c++ - 在运行时确定的固定大小数组的 vector

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:40 24 4
gpt4 key购买 nike

我正在尝试做这样的事情:

int x=128, y=256;
std::vector<std::array<float,x*y>> codes;

显然这是错误的,而这是正确的:

std::vector<std::array<float,128*256>> codes;

第一个问题的一个解决方案是使用如下宏:

#define x 128
#define y 256
...
std::vector<std::array<float,x*y>> codes;

但我想知道在运行时是否有另一种解决方案而不是编译时。 注意 没有必要使用std::array,我需要一个std::vectorx 数组(或其他) *y 元素。

最佳答案

尝试使用const:

const int x = 128;
const int y = 256;
...
std::vector<std::array<float,x*y>> codes;

您的第一个代码中的问题在于 xy 不是常量,因此编译器不能依赖于它们的值不会改变的事实运行时,因此无法在编译时确定模板参数值。

关于c++ - 在运行时确定的固定大小数组的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40991222/

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