2

I know when we want to assign values to 2D arrays as we declare the array, we do this:

int myArray[2][4] = {{1,2,3,4},{5,6,7,8}};

But how should I assign values "after" declaring it? I want to do something like this:

int myArray[2][4];

myArray = {{1,2,3,4},{5,6,7,8}};

When I do it, the compiler gives error. Help please.

Reza Hajianpour
  • 707
  • 1
  • 10
  • 21
  • see http://stackoverflow.com/questions/8886375/possible-to-initialize-an-array-after-the-declaration-in-c – zakinster Apr 17 '13 at 12:19
  • 3
    Plain arrays are not assignable, so you have to fill in each element individually. You could consider using a standard library container, such as `std::array`. – juanchopanza Apr 17 '13 at 12:19
  • 1
    Can you use C++11 and `std::vector` instead? – Shafik Yaghmour Apr 17 '13 at 12:22
  • I've used the array in my code too much. So I can't replace it with anything at all. :( – Reza Hajianpour Apr 17 '13 at 12:24
  • `Refactoring` is your friend and, if done carefully, can save you time as opposed to manually making the replacements from `myArray[2][4]` to `std::vector > myArray;` If you are using an IDE that supports `refactoring` and `find and replace`, then you should be good to go. – Mushy Apr 17 '13 at 12:30
  • 1
    @Mushy I would say the better replacement here is `std::array,2>`. This keeps the elements in contiguous memory and has no overhead. – juanchopanza Apr 17 '13 at 12:34
  • @juanchopanza How to create a 2D vector then? With 2 rows and 4 columns? – Reza Hajianpour Apr 17 '13 at 12:47
  • @juanchopanza nevermind, figured it out – Shafik Yaghmour Apr 17 '13 at 12:55
  • OK, I added an example anyway. – juanchopanza Apr 17 '13 at 13:10
  • @juanchopanza The OP: `But how should I assign values "after" declaring it?` `std::array` is fixed like built-in array and so no, you can't do what the OP desires. – Mushy Apr 17 '13 at 14:09
  • @Mushy you can, as in my example. – juanchopanza Apr 17 '13 at 14:13
  • @zakinster Actually, I just realized the question you point to has `C` label this is a `C++` question and thus the answers are actually different – Shafik Yaghmour May 03 '13 at 15:17
  • @ShafikYaghmour Since `C++03` doesn't provide any more alternatives than `C89` on this matter, the answers are only different if we include the `C++11` alternatives. The real question is "Does the `C++` tag includes `C++11` ?" If yes, then this answer is definitely not a duplicate. – zakinster May 03 '13 at 16:14
  • @zakinster The `C++` tag does indeed cover `C++11`, although the answer should note that it is `C+11` specific. – Shafik Yaghmour May 03 '13 at 17:20

1 Answers1

2

If you want to use std::vector then you can do this:

#include <vector>

int main()
{
    std::vector< std::vector<int> > arrV ;

    arrV = { {1,2,3,4}, {5,6,7,8} };
}

or using std::array:

#include <array>

int main()
{
    std::array<std::array<int,4>,2> arr ;

    arr =  {{  {{1,2,3,4 }}, {{5,6,7,8}}  }} ;
}

Note, the double set of braces in both the inner and outer set. This answer though only works in C++11.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740