Vector< ElemType > Class Template Reference

#include "vector.h"

List of all members.

Public Member Functions

 Vector (int sizeHint=0)
 ~Vector ()
int size ()
bool isEmpty ()
ElemType getAt (int index)
void setAt (int index, ElemType value)
ElemType & operator[] (int index)
void add (ElemType elem)
void insertAt (int index, ElemType elem)
void removeAt (int index)
void clear ()
const Vectoroperator= (const Vector &rhs)
 Vector (const Vector &rhs)


Detailed Description

template<typename ElemType>
class Vector< ElemType >

This interface defines a class template that stores a homogeneous indexed collection. The basic operations are similar to those in the built-in array type, with the added features of dynamic memory management, bounds-checking on indexes, and convenient insert/remove operations. Like an array, but better! For maximum generality, the Vector is supplied as a class template. The client specializes the vector to hold values of a specific type, e.g. Vector<int> or Vector<studentT>, as needed


Constructor & Destructor Documentation

template<typename ElemType>
Vector< ElemType >::Vector ( int  sizeHint = 0  )  [explicit]

The constructor initializes a new empty vector. The optional argument is a hint about the expected number of elements that this vector will hold, which allows vector to configure itself for that capacity during initialization. If not specified, it is initialized with default capacity and grows as elements are added. Note that capacity does NOT mean size, a newly constructed vector always has size() = 0. A large starting capacity allows you to add that many elements without requiring any internal reallocation. The explicit keyword is required to avoid accidental construction of a vector from an int.

template<typename ElemType>
Vector< ElemType >::~Vector (  ) 

The destructor deallocates storage associated with this vector.

template<typename ElemType>
Vector< ElemType >::Vector ( const Vector< ElemType > &  rhs  ) 


Member Function Documentation

template<typename ElemType>
int Vector< ElemType >::size (  )  [inline]

This member function returns the number of elements in this vector.

template<typename ElemType>
bool Vector< ElemType >::isEmpty (  ) 

This member function returns true if this vector contains no elements, false otherwise.

template<typename ElemType>
ElemType Vector< ElemType >::getAt ( int  index  ) 

This member function returns the element at the specified index in this vector. Elements are indexed starting from 0. A call to vec.getAt(0) returns the first element, vec.getAt(vec.size()-1) returns the last. Raises an error if index is outside the range [0, size()-1].

template<typename ElemType>
void Vector< ElemType >::setAt ( int  index,
ElemType  value 
)

This member function replaces the element at the specified index in this vector with a new value. The previous value at that index is overwritten with the new value. The size of the vector is unchanged. Raises an error if index is not within the range [0, size()-1].

template<typename ElemType>
ElemType & Vector< ElemType >::operator[] ( int  index  )  [inline]

This member function overloads [] to access elements from this vector. This allows the client to use array-like notation to get/set individual vector elements. Returns a reference to the element to allow in-place modification of values. Raises an error if index is not within the range [0, size()-1].

template<typename ElemType>
void Vector< ElemType >::add ( ElemType  elem  ) 

This member function adds an element to the end of this vector. The vector's size increases by one.

template<typename ElemType>
void Vector< ElemType >::insertAt ( int  index,
ElemType  elem 
)

This member function inserts the element into this vector at the specified index, shifting all subsequent elements one index higher. A call to vec.insertAt(0, val) inserts a new element at the beginning, vec.insertAt(vec.size(), val) add a new element to the end. The vector's size increases by one. Raises an error if index is outside the range [0, size()].

template<typename ElemType>
void Vector< ElemType >::removeAt ( int  index  ) 

This member function removes the element at the specified index from this vector, shifting all subsequent elements one index lower. A call to vec.removeAt(0) removes the first element, vec.removeAt(vec.size()-1), removes the last. The vector's size decreases by one. Raises an error if index is outside the range [0, size()-1].

template<typename ElemType>
void Vector< ElemType >::clear (  ) 

This member function removes all elements from this vector. The vector is made empty and will have size() = 0.

template<typename ElemType>
const Vector< ElemType > & Vector< ElemType >::operator= ( const Vector< ElemType > &  rhs  ) 

This copy constructor and operator= are defined to make a deep copy, making it possible to pass/return vectors by value and assign from one vector to another. The entire contents of the vector, including all elements, are copied. Each vector element is copied from the original vector to the copy using assignment (operator=). Making copies is generally avoided because of the expense and thus, vectors are typically passed by reference, however, when a copy is needed, these operations are supported.


The documentation for this class was generated from the following file:
Generated on Mon Feb 12 23:30:08 2007 for CS 106B Libraries by  doxygen 1.5.1