Jump to content
  • Advertisement
Sign in to follow this  

Array in array, how to fill with content?

This topic is 2309 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:

array<array<int>> arr;
arr[0] = {0};

gets me a:

ERR : Expected expression value

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
these all work.

array<array<int>> arr;
arr[0][0] = 52;

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.

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[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

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

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!