Public Member Functions | Private Member Functions | Private Attributes | List of all members
Vector< T > Class Template Reference

#include <vector.h>

Public Member Functions

 Vector ()
 
 Vector (const unsigned) throw ( bad_alloc )
 
 Vector (const unsigned, const T &) throw ( bad_alloc )
 
 Vector (const Vector< T > &) throw ( bad_alloc )
 
 ~Vector ()
 
void assign (const unsigned, const T &) throw ( out_of_range )
 
T & at (const unsigned) const throw ( out_of_range )
 
unsigned capacity () const
 
void clear ()
 
bool empty () const
 
void insert (const unsigned, const T &) throw ( bad_alloc, out_of_range )
 
T & operator[] (const unsigned) const throw ( out_of_range )
 
const Vector< T > & operator= (const Vector< T > &) throw ( bad_alloc )
 
void push_back (const T &) throw ( bad_alloc )
 
void resize (const unsigned, const T &=T()) throw ( bad_alloc )
 
unsigned size () const
 
void sort ()
 
void remove (const unsigned) throw ( out_of_range )
 

Private Member Functions

void increaseCapacity () throw ( bad_alloc )
 

Private Attributes

T * contents
 
unsigned sz
 
unsigned cap
 

Detailed Description

template<typename T>
class Vector< T >

Implementation of a resizable Vector ADT using a dynamically allocated C-style array.

Author
Mark Maloof (maloo.nosp@m.f@cs.nosp@m..geor.nosp@m.geto.nosp@m.wn.ed.nosp@m.u)
Version
1.3, 5/7/13

Constructor & Destructor Documentation

template<typename T >
Vector< T >::Vector ( )

Default constructor. Constructs an empty vector with an initial capacity of ten.

template<typename T >
Vector< T >::Vector ( const unsigned  cap)
throw (bad_alloc
)

Constructor for initializing an empty vector with a specified capacity.

Parameters
capthe capacity of the vector
Exceptions
bad_allocif memory cannot be allocated
template<typename T >
Vector< T >::Vector ( const unsigned  sz,
const T &  object 
)
throw (bad_alloc
)

Constructor for initializing a vector to an initial size with its components initialized to a specified object.

Parameters
szthe size of the vector
objectthe initial object for the components
Exceptions
bad_allocif memory cannot be allocated
template<typename T >
Vector< T >::Vector ( const Vector< T > &  vector)
throw (bad_alloc
)

Copy constructor. Makes a deep copy of the specified vector.

Parameters
vectorthe vector to be copied
Exceptions
bad_allocif memory cannot be allocated
template<typename T >
Vector< T >::~Vector ( )

Destructor.

Member Function Documentation

template<typename T >
void Vector< T >::assign ( const unsigned  i,
const T &  object 
)
throw (out_of_range
)

Assigns the object to the specified position in this vector.

Parameters
ithe position to be assigned.
objectthe object to be stored in this vector.
Exceptions
out_of_rangeif index parameter is out of bounds.
template<typename T >
T & Vector< T >::at ( const unsigned  i) const
throw (out_of_range
)

Returns a reference to the object stored at a given position in this vector.

Parameters
ithe object's location.
Returns
a reference to the object.
Exceptions
out_of_rangeif index parameter is out of bounds.
template<typename T >
unsigned Vector< T >::capacity ( ) const

Returns the capacity of the vector, which is the number of elements that the vector can store before increasing the capacity.

Returns
an unsigned integer indicating the vector's capacity.
template<typename T >
void Vector< T >::clear ( )

Removes the elements of the vector.

template<typename T >
bool Vector< T >::empty ( ) const

Returns true if the vector is empty; returns false otherwise.

Returns
true if empty; false otherwise.
template<typename T >
void Vector< T >::increaseCapacity ( )
throw (bad_alloc
)
private

Increases the capacity of the vector by doubling its current capacity.

Exceptions
bad_allocif memory cannot be allocated.
template<typename T >
void Vector< T >::insert ( const unsigned  i,
const T &  object 
)
throw ( bad_alloc,
out_of_range
)

Inserts the object at the given position. Increases capacity if necessary.

Parameters
ithe position of insertion.
objectthe object to be inserted.
Exceptions
bad_allocif memory cannot be allocated.
out_of_rangeif index parameter is out of bounds.
template<typename T >
const Vector< T > & Vector< T >::operator= ( const Vector< T > &  vector)
throw (bad_alloc
)

Returns a deep copy of the vector passed in as the parameter.

Parameters
vectorthe vector to be copied.
Returns
a copy of the vector.
Exceptions
bad_allocif memory cannot be allocated.
template<typename T >
T & Vector< T >::operator[] ( const unsigned  i) const
throw (out_of_range
)

Returns a reference to the object stored at a given position in this vector.

Parameters
ithe object's location.
Returns
a reference to the object.
Exceptions
out_of_rangeif index parameter is out of bounds.
template<typename T >
void Vector< T >::push_back ( const T &  object)
throw (bad_alloc
)

Adds the object to the end of the vector. Increases capacity if necessary.

Parameters
objectthe object to be added to the end of the vector.
Exceptions
bad_allocif memory cannot be allocated.
template<typename T >
void Vector< T >::remove ( const unsigned  i)
throw (out_of_range
)

Removes the object stored in the given position.

Parameters
ithe position of removal.
Exceptions
out_of_rangeif index parameter is out of bounds.
template<typename T >
void Vector< T >::resize ( const unsigned  newSize,
const T &  object = T() 
)
throw (bad_alloc
)

Resizes the vector to its new size. After allocating new memory and copy the contents of old memory, stores the object in any unassigned locations.

Parameters
newSizethe new size of the vector
objectthe object for any new, unassigned locations
Exceptions
bad_allocif memory cannot be allocated
template<typename T >
unsigned Vector< T >::size ( ) const

Returns the size (i.e., the number of elements) of the vector.

Returns
an unsigned integer indicating the vector's size.
template<typename T >
void Vector< T >::sort ( )

Sorts the elements of this vector in ascending order.

Member Data Documentation

template<typename T >
unsigned Vector< T >::cap
private

the capacity of this vector

template<typename T >
T* Vector< T >::contents
private

the contents of this vector

template<typename T >
unsigned Vector< T >::sz
private

the size of this vector


The documentation for this class was generated from the following file: