2015-04-16 73 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托特歡迎您。 –