将数组值(行)添加到二维数组 C++

Add an array value (row) to a 2d array C++

我有一个 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;
}

喜欢:

for (int i = 0; i < 10; ++i){
   a[i][] = [a,b]; //or something like this
}

谢谢! :)

数组没有赋值运算符。但是,您可以使用 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;
    }
}

输出为

1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2