#include "vector.h"

class Vector<ValueType>

This class stores an ordered list of values similar to an array. It supports traditional array selection using square brackets, but also supports inserting and deleting elements. It is similar in function to the STL vector type, but is simpler both to use and to implement.
Constructor
Vector()
Vector(n, value) 
Initializes a new vector.
Methods
add(value) Adds a new value to the end of this vector.
clear() Removes all elements from this vector.
get(index) Returns the element at the specified index in this vector.
insert(0, value) Inserts the element into this vector before the specified index.
isEmpty() Returns true if this vector contains no elements.
mapAll(fn) Calls the specified function on each element of the vector in ascending index order.
remove(index) Removes the element at the specified index from this vector.
set(index, value) Replaces the element at the specified index in this vector with a new value.
size() Returns the number of elements in this vector.
toString() Converts the vector to a printable string representation.
Operators
v1 + v2 Concatenates two vectors.
v1 += v2; Adds all of the elements from v2 (or the single specified value) to v1.
vec.mapAll(fn); Adds an element to the vector passed as the left-hand operatand.
vec[index] Overloads [] to select elements from this vector.

Constructor detail


Vector();
Vector(int n, ValueType value = ValueType());
Initializes a new vector. The default constructor creates an empty vector. The second form creates an array with n elements, each of which is initialized to value; if value is missing, the elements are initialized to the default value for the type.

Usage:

Vector<ValueType> vec;
Vector<ValueType> vec(n, value);

Method detail


int size() const;
Returns the number of elements in this vector.

Usage:

int nElems = vec.size();

bool isEmpty() const;
Returns true if this vector contains no elements.

Usage:

if (vec.isEmpty()) ...

void clear();
Removes all elements from this vector.

Usage:

vec.clear();

const ValueType & get(int index) const;
Returns the element at the specified index in this vector. This method signals an error if the index is not in the array range.

Usage:

ValueType val = vec.get(index);

void set(int index, const ValueType & value);
Replaces the element at the specified index in this vector with a new value. The previous value at that index is overwritten. This method signals an error if the index is not in the array range.

Usage:

vec.set(index, value);

void insert(int index, ValueType value);
void insertAt(int index, ValueType value);
Inserts the element into this vector before the specified index. All subsequent elements are shifted one position to the right. This method signals an error if the index is outside the range from 0 up to and including the length of the vector. To maintain compatibility with older code, this method may also be called as insertAt.

Usage:

vec.insert(0, value);

void remove(int index);
void removeAt(int index);
Removes the element at the specified index from this vector. All subsequent elements are shifted one position to the left. This method signals an error if the index is outside the array range. To maintain compatibility with older code, this method may also be called as removeAt.

Usage:

vec.remove(index);

void add(ValueType value);
void push_back(ValueType value);
Adds a new value to the end of this vector. To ensure compatibility with the vector class in the Standard Template Library, this method is also called push_back.

Usage:

vec.add(value);

string toString();
Converts the vector to a printable string representation.

Usage:

string str = vec.toString();

void mapAll(void (*fn)(ValueType)) const;
void mapAll(void (*fn)(const ValueType &)) const;
void mapAll(FunctorType fn) const;
Calls the specified function on each element of the vector in ascending index order.

Usage:

vec.mapAll(fn);

Operator detail


ValueType & operator[](int index);
const ValueType & operator[](int index) const;
Overloads [] to select elements from this vector. This extension enables the use of traditional array notation to get or set individual elements. This method signals an error if the index is outside the array range. The file supports two versions of this operator, one for const vectors and one for mutable vectors.

Usage:

vec[index]

Vector operator+(const Vector & v2) const;
Concatenates two vectors.

Usage:

v1 + v2

Vector & operator+=(const Vector & v2);
Vector & operator+=(const ValueType & value);
Adds all of the elements from v2 (or the single specified value) to v1. As a convenience, the Vector package also overloads the comma operator so that it is possible to initialize a vector like this:
   Vector<int> digits;
   digits += 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;

Usage:

v1 += v2;
v1 += value;

Vector & operator,(const ValueType & value);
Adds an element to the vector passed as the left-hand operatand. This form makes it easier to initialize vectors in old versions of C++.

Usage:

vec.mapAll(fn);