push_back

( <-- Back to Sporkl Function Library )


void push_back ( array [any_base_type] & arr, [any_base_type] val )


Appends an element to the end of an array.



Arguments:

arr:
val:
Array to append element to end of
Value of element to append to end of arr

Returns:

Nothing.

Notes:

The type of argument val must either be the same type as the base type of arr, or must be able to be converted to arr's base type, otherwise a compiler error will be generated.

Example:

The code:

array int a;

print_array_contents(a);

print("Pushing back one value...\n\n");
push_back(a,5);

print_array_contents(a);

print("Pushing back two values...\n\n");
push_back(a,4);
push_back(a,3);

print_array_contents(a);


function void print_array_contents ( array int & x )
{
   int i;
   for ( i=0; i<size(x); ++i )
   {
      print("  " + str(i) + ": " + str(x[i]) + "\n");
   }
   print("(size=" + str(size(x)) + ")\n\n");
}

produces the following output:

(size=0)

Pushing back one value...

  0: 5
(size=1)

Pushing back two values...

  0: 5
  1: 4
  2: 3
(size=3)









sporkforge