#include "vector.h"
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 Vector & | operator= (const Vector &rhs) |
| Vector (const Vector &rhs) | |
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.
The destructor deallocates storage associated with this vector.
| int Vector< ElemType >::size | ( | ) | [inline] |
This member function returns the number of elements in this vector.
| bool Vector< ElemType >::isEmpty | ( | ) |
This member function returns true if this vector contains no elements, false otherwise.
| 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].
| 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].
| 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].
| 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.
| 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()].
| 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].
| void Vector< ElemType >::clear | ( | ) |
This member function removes all elements from this vector. The vector is made empty and will have size() = 0.
| 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.
1.5.1