Skip to content

vectors

bergsma edited this page Jul 31, 2014 · 1 revision

Vectors

A vector is a fixed array of identically typed values. The vector representation is internally more compact and access to individual elements is far more efficient than accessing elements of a list or str variable. Vector data types include: _char, uchar, binary, boolean, byte, ubyte, short, ushort, long, ulong, float, double, int _and handle.

The following statement creates a vector of 5 integers:

int a = { 1, 2, 3, 4, 5 };        /* A vector of integers */

_

Whereas the following two statements create lists, not vectors:

list a = { 1, 2, 3, 4, 5 };        /* A list of integers */
b = { 6, 7, 8, 9, 10 };         /* A list of integers */

_

If a vector is not initialized, all the values get initialized to zero.

float v[10];        /* A vector of 10 floats,
                   all values initialized to 0.0 */

_

A vectors' values can be initialized at any time:

v = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0 };

_

However, once the size of a vector variable is specified either through initialization or declaration, it cannot change unless the vector variable is re-declared or the vector variable has a size of 1. The following examples illustrates this:

float x = 99;        	/* x initialized with 1 value */
x = { 99, 100, 101 };        /* OK, x now has 3 values */
float x[5];        	/* OK, x re-declared and now has 5 values */

_

Note that the first three values of x are preserved in the re-declaration statement; the values of x are now { 99, 100, 101, 0, 0 }. If we perform the following assignment:

x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };        /* Watch out! x still has 5 values! */

_

The first 5 values in x will change, but no other values will be appended; the values of x are now { 1, 2, 3, 4, 5 }. A re-declaration of x will be necessary to change the size.

Clone this wiki locally