Turok
 
Loading...
Searching...
No Matches
array< T > Class Reference

Detailed Description

It is possible to declare array variables with the array identifier followed by the type of the elements within angle brackets.

Example:

a, b, and c are now arrays of integers, and d is an array of handles to objects of the Foo type.

When declaring arrays it is possible to define the initial size of the array by passing the length as a parameter to the constructor. The elements can also be individually initialized by specifying an initialization list. Example:

array<int> a; // A zero-length array of integers
array<int> b(3); // An array of integers with 3 elements
array<int> c(3, 1); // An array of integers with 3 elements, all set to 1 by default
array<int> d = {5,6,7}; // An array of integers with 3 elements with specific values

Multidimensional arrays are supported as arrays of arrays, for example:

array<array<int>> a; // An empty array of arrays of integers
array<array<int>> b = {{1,2},{3,4}} // A 2 by 2 array with initialized values
array<array<int>> c(10, array<int>(10)); // A 10 by 10 array of integers with uninitialized values

Each element in the array is accessed with the indexing operator. The indices are zero based, i.e. the range of valid indices are from 0 to length - 1.

a[0] = some_value;

When the array stores handles the elements are assigned using the handle assignment.

// Declare an array with initial length 1
array<Foo@> arr(1);
// Set the first element to point to a new instance of Foo
@arr[0] = Foo();

Arrays can also be created and initialized within expressions as anonymous objects.

// Call a function that expects an array of integers as input
foo(array<int> = {1,2,3,4});