C ++分配和使用常量作为初始化程序

时间:2018-03-13 13:45:26

标签: c++ arrays multidimensional-array const

请在下面找到我的代码段:

module.exports = {
    gretting (req, res, next) {
        Driver.find({ "theater": "TodayTainan" })
            .then(movie => res.send(movie))
            .catch(next);
    }

请在使用C ++和clang ++进行编译时找到错误。

std::vector<int> idx1;
std::vector<int> idx2;
framx[0].get_idx_of(atm1,idx1);
framx[0].get_idx_of(atm2,idx2);
std::vector<bool> iniz;
const int sz1=idx1.size();
const int sz2=idx2.size();
array<array<array<double,7>,sz2>,sz1> lnmat;

在转让变量lnmat时,能告诉我这个错误吗?似乎数组声明似乎没有识别常量,或者它的声明存在一些问题。

P.S。:请不要回答this问题。

1 个答案:

答案 0 :(得分:0)

std::array必须在编译时知道第二个模板参数。

您正在使用仅在运行时知道的常量,即:

const int sz1=idx1.size();
const int sz2=idx2.size();

所以std::array<array<array<double,7>,sz2>,sz1> lnmat;无效。

除非您在编译时知道sz1sz2的值(运行程序之前),否则无法使用std::array

您可能希望改用std::vector。与std::vector<std::vector<std::array<double,7>>> lnmat;类似,但请注意,与std::array不同,向量在初始化时为空。

同样正如@NathanOliver所提到的,对于多维数组使用向量向量可能效率不高(因为缓存局部性),如果您的代码对性能敏感,请考虑使用可用作多维数组的单个std::vector ,更多细节here