Array in array, how to fill with content?

Started by
2 comments, last by 93i 11 years, 8 months ago
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
[/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?
Advertisement
these all work.

array<array<int>> arr;
arr.resize(10);
arr[0].resize(10);
arr[0].insertLast(0);
arr[0][0] = 52;
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[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


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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanx

This topic is closed to new replies.

Advertisement