The fixed size of arrays is often a difficult programming constraint and
frequently the source of bugs. The STL (Standard Template Library)
vector
class is a dynamically expandable array.
The floatVector
example here
shows how vector
is implemented, but without the
distraction of templates and iterators of the STL vector
class.
The code is divided into three files, one for testing, one containing the header
file, and one which has the implementation of the methods.
This first version is a very small subset of the features of the actual vector
class.
Many improvements can be made to this class.
bool empty() const; //True if no elements, size()==0. float& front() const; //Returns reference to first element void pop_back(); //Remove last element.They should all do the same thing as the corresponding STL: vector<T> template class functions. Modify the test program to test them.
at
function in the vector
class throws
the out_of_range
exception if the subscript is
out of range. Change at
to do that, but also
do the same for subscription and throw an exception in the
constructor if the size isn't greater than zero.
See Exceptions.
at()
function. The equality operator (==
) should first compare the sizes.
If the sizes are unequal, return false. If the sizes are equal, it's necessary
to loop thru the data. If any corresponding elements are unequal, return
false.
float& operator[](int position) const; bool operator==(const floatVector& v2) const;
floatVector& operator=(const floatVector& fv); floatVector(const floatVector& fv); // Copy constructor. ~floatVector(); // Destructor.
operator<<
as a friend
function.
See Overloading << and >>
{1.1, 7.3345, 3.14}
friend ostream& operator<<(ostream& os, const floatVector& fv);
xvector
to distinguish it from the standard vector
class.
Use the test program Test Program for xvector.
You may find the following useful for testing solutions to the first 4 problems too, but you will have to comment out the tests and prototypes that you haven't yet implemented..