【问题标题】:C++ assigning and using constant as initializer [duplicate]C ++分配和使用常量作为初始化程序[重复]
【发布时间】:2018-03-13 13:45:26
【问题描述】:

请在下面找到我的代码 sn-p:

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;

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

analysis.cpp:408:33: error: the value of ‘sz2’ is not usable in a constant expression
     array<array<array<double,7>,sz2>,sz1> lnmat;
                                 ^~~
analysis.cpp:407:15: note: ‘sz2’ was not initialized with a constant expression
     const int sz2=idx2.size();
               ^~~
analysis.cpp:408:36: error: the value of ‘sz2’ is not usable in a constant expression
     array<array<array<double,7>,sz2>,sz1> lnmat;
                                    ^
analysis.cpp:407:15: note: ‘sz2’ was not initialized with a constant expression
     const int sz2=idx2.size();
               ^~~
analysis.cpp:408:36: note: in template argument for type ‘long unsigned int’ 
     array<array<array<double,7>,sz2>,sz1> lnmat;
                                    ^
analysis.cpp:408:38: error: the value of ‘sz1’ is not usable in a constant expression
     array<array<array<double,7>,sz2>,sz1> lnmat;
                                      ^~~
analysis.cpp:406:15: note: ‘sz1’ was not initialized with a constant expression
     const int sz1=idx1.size();
               ^~~
analysis.cpp:408:41: error: template argument 1 is invalid
     array<array<array<double,7>,sz2>,sz1> lnmat;
                                         ^
analysis.cpp:408:41: error: the value of ‘sz1’ is not usable in a constant expression
analysis.cpp:406:15: note: ‘sz1’ was not initialized with a constant expression
     const int sz1=idx1.size();
               ^~~
analysis.cpp:408:41: note: in template argument for type ‘long unsigned int’ 
     array<array<array<double,7>,sz2>,sz1> lnmat;

您能否告诉我在分配变量 lnmat 时出现此错误的原因?看起来可能是数组声明没有识别常量,或者它的声明有问题。

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

【问题讨论】:

  • 您能解释一下为什么您认为链接的问题不相关吗?你明白为什么你的向量的大小在编译时通常不可用吗?
  • 模板参数必须在编译时知道。 const 不保证这一点,就像您对 sz1sz2 的情况一样。 const 仅表示对象是不可变的。在 C++11 中,有 constexpr 来定义编译时常量。但是,您仍然无法为其分配运行时值。

标签: c++ arrays multidimensional-array constants


【解决方案1】:

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

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

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

所以std::array&lt;array&lt;array&lt;double,7&gt;,sz2&gt;,sz1&gt; lnmat; 无效。

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

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

也正如@NathanOliver 提到的,对多维数组使用向量的向量可能不是很有效(因为缓存局部性),如果您的代码对性能敏感,请考虑使用可用作多维数组的单个std::vector,更多详情here.

【讨论】:

  • 您能告诉我在这种情况下如何处理吗?我应该改用矢量吗?
  • 是的,使用std::vector。请注意,为了提高效率(和更好的内存使用),用户可能希望定义一个普通的 1-D std::vector&lt;double&gt; lnmat(sz1*sz2*7); 并将其用作 3-D 数组。
  • @MitradipDas 除非您在编译时知道 sz1 和 sz2 的值是什么(在运行程序之前),否则您不能使用 std::array。改用 std::vector 。像 std::vector<:vector>>> lnmat;
  • @RandomGuy 不建议使用嵌套向量。它们在嵌套向量数据之间没有保证的位置。在平面向量中抽象二维结构是首选方法。
  • 感谢@NathanOliver,我已将您的替代方案添加到答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 2020-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多