#include "vector.tcc"

A Vector is a sequence container that implements storage of data elements. The type of the vector is given at initialisation time via a template parameter, e.g., int.

Vector<int> v;
Vector<int> u(12);

In this example, Vector v is of size 0 and u is of size 12. A Vector can also be initialised with a pointer to an allocated block of memory.

Vector<int> v(12, data);

The memory block is freed when the Vector is destroyed. If this is not desirable, an additional flag destroy can be passed to the constructor as follows.

Vector<int> v(12, data, false);

This behaviour can also be changed by manipulating the destroy member variable.

A Vector can be resized using the resize method.


The size member variable contains the current size of the Vector.

Element retrieval and assignment is done in the usual way.

int i = v[10];

v[11] = 9;

Note that a Vector, like any higher order data structure, should be passed by reference.

Class definition

template <class T>

Generic Vector.

Public Functions

Vector::Vector(size_t size)

Create a Vector with size elements.


Vector::Vector(size_t size, T *data, bool destroy = true)

Create a Vector with size elements from a C array.

  • size -

    Size of the Vector.

  • data -

    Pointer to data.

  • destroy -

    Free data in the destructor.



void Vector::resize(size_t size)

Resize the Vector.

  • size -

    New size of the Vector.

T &Vector::operator[](size_t index)

Get a reference to an element.

This can be used for both retrieval as well as assignment.

Reference to element at index index.
  • index -


Public Members

size_t Vector::size

Number of elements.

bool Vector::destroy

Free memory when destructor is called.