• Advertisement
Sign in to follow this  

Array in array, how to fill with content?

This topic is 1982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i want to use a array in array, but none of the ways in the docs seem to work for me:

For example:
[code]
array<array<int>> arr;
arr[0] = {0};
[/code]

gets me a:

[quote]
ERR : Expected expression value
[/quote]

I tried other ways, like arr[0].insertLast(0); or even arr.resize(1); arr[0]...; none seems to work.

How is it supposed to work?

Share this post


Link to post
Share on other sites
Advertisement
these all work.
[CODE]
array<array<int>> arr;
arr.resize(10);
arr[0].resize(10);
arr[0].insertLast(0);
arr[0][0] = 52;
[/CODE]

Share this post


Link to post
Share on other sites
The initialization list can only be used in variable declarations, i.e. you can't use it in ordinary assignment expressions like a[0] = {0}.

You have to remember that an array of arrays is just that, an array of arrays. The elements in the first array are all individual arrays of the defined element type. A change on one of the subarrays doesn't automatically reflect on the other subarrays.

[code]
array<array<int>> arr1 = {{0,0}, {1,1}}; // a 2 by 2 array, fully initialized

array<array<int>> arr2(2); // a 2 by 0 array
arr2[0] = array<int>(2, 0); // first row is now 0, 0
arr2[1] = array<int>(2, 1); // second row is now 1, 1

array<array<int>> arr3(2, array<int>(2, 0)); // a 2 by 2 array. all elements are 0
arr3[1] = array<int>(2, 1); // fully initialized

array<array<int>> arr4; // a 0 by 0 array
arr4.resize(2); // a 2 by 0 array
arr4[0].resize(2);
arr4[1].resize(2); // a 2 by 2 array
arr4[0][0] = 0;
arr4[0][1] = 0;
arr4[1][0] = 1;
arr4[1][1] = 1; // fully initialized
[/code]

All four of these produce the same result, a 2x2 array where the first row is 0,0, and the second row is 1,1.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement