2015-04-16 76 views
0

我有一个int a [10] [2]数组。我可以用其他方式分配的值是这样的:将数组值(行)添加到二维数组C++

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 
} 

谢谢! :)

回答

4

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

谢谢,它的工作:) – Totati

+0

@Attila托特欢迎您。 –