0xb*_*7ed 3 c++ compiler-errors constants constant-expression c++11
我有以下数组:
int const A[4] = { 0, 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)
我想初始化一个重复的数组,如下所示:
int a[4] = A;
Run Code Online (Sandbox Code Playgroud)
如果我在cygwin上运行g ++ 4.8.2如下:
g++ --std=c++11 myfile.cpp
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
myfile.cpp:16:16: error: array must be initialized with a brace-enclosed initializer
int a[4] = A;
^
Run Code Online (Sandbox Code Playgroud)
然而,显然" int a[4] = { A };"也不会起作用.有没有一种方法来初始化我的数组a从A使用简单的赋值语句,而不诉诸:
int a[4] = { A[0], A[1], A[2], A[3] };
Run Code Online (Sandbox Code Playgroud)
?
std::copy(A, A+4, a)
Run Code Online (Sandbox Code Playgroud)
或者,通过使用std :: array具有您想要的简单复制方法:
std::array<int, 4>A = {0, 1, 2, 3}
std::array<int, 4>a = A;
Run Code Online (Sandbox Code Playgroud)
使用标准类std::array.
#include <array>
//...
const std::array<int, 4> A = { 0, 1, 2, 3 };
std::array<int, 4 > a = A;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
475 次 |
| 最近记录: |