0

Looking for a C++11/14 syntax where I can assign a computed value to each cell of the inner array for my 2D array (the outer array being an array for those inner lines):

void foo(std::array<std::array<char, NCOLS>, NROWS>& other_array) {
  float value = 42.0;
  std::array<std::array<float, other_array[0].size()>, 

other_array.size()> new_array; // HOW DO I ASSIGN value to all elements?
  // .../...
}
John Difool
  • 5,572
  • 5
  • 45
  • 80
  • I'm sure there's something for this in the [algorithms library](http://en.cppreference.com/w/cpp/algorithm). – Captain Obvlious Jul 03 '15 at 19:42
  • The problem with this question is that, it is VERY likely that you are not the first person with this [problem](http://stackoverflow.com/questions/1065774/c-c-initialization-of-a-normal-array-with-one-default-value). So this looks like you did not put enough effort into searching for similar problems before asking your question. To remove this suspicion you could have posted links to similar questions and explained how your use-cas is different. – RedX Jul 03 '15 at 19:47

1 Answers1

1
for (auto& row : new_array)
  std::fill(row.begin(), row.end(), value);
Chris Drew
  • 14,926
  • 3
  • 34
  • 54
  • Thank you! I looked for something similar and ended up trying: std::fill_n(new_array, NCOLS*NROWS, value) but that doesn't seem to work for dimensions above 1. Is that true? – John Difool Jul 03 '15 at 20:10
  • 1
    @JohnDifool It might be possible to write something like that but I'm not convinced it would be portable. – Chris Drew Jul 03 '15 at 20:29