Skip to main content

๐Ÿ“ Operator[]

Returns the requested element at the relative position of the array (the first index is aways the most recent element added).

If the requested index exceed the array size, it will return a 0.

๐Ÿ“ Syntaxโ€‹

TypeOfArray operator[](int index)

๐Ÿ”ฎ Exampleโ€‹

// This will create an MovingAverage of size 4 and int type
DataTomeMvAvg<int> intAverage(4);

// Array[]: 1 0 0 0
intAverage.push(1);
// Array[]: 2 1 0 0
intAverage.push(2);
// Array[]: 3 2 1 0
intAverage.push(3);
// Array[]: 4 3 2 1
intAverage.push(4);

// Return 4
intAverage[0];

โฑ Complexityโ€‹

Constant (O(1)).