我有一个 int a[10][2] 数组。我可以用其他方式赋值吗,如下所示:
int a = someVariableValue;
int b = anotherVariableValue;
for (int i = 0; i < 10; ++i){
a[i][0] = a;
a[i][1] = b;
}
Run Code Online (Sandbox Code Playgroud)
喜欢:
for (int i = 0; i < 10; ++i){
a[i][] = [a,b]; //or something like this
}
Run Code Online (Sandbox Code Playgroud)
谢谢你!:)
数组没有赋值运算符。但是您可以使用std::array
.
例如
#include <iostream>
#include <array>
int main()
{
const size_t N = 10;
std::array<int, 2> a[N];
int x = 1, y = 2;
for ( size_t i = 0; i < N; ++i ) a[i] = { x, y };
for ( const auto &row : a )
{
std::cout << row[0] << ' ' << row[1] << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
输出是
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
Run Code Online (Sandbox Code Playgroud)