Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Vector.h defines a vector template class. The class is very similar to the STL vector class. The file dsexceptions.h defines three exception classes which have

Vector.h defines a vector template class. The class is very similar to the STL vector class. The file dsexceptions.h defines three exception classes which have no content but is required by the program. The file TestVector.cpp is the driver program. It tests the vectorclass in vector.h.Compile the program TestVector.cpp and test it. Make sure it works. For this assignment you will add the methods erase and insert to the vector class. You will have to find out the parameters (signature) of these methods and the functionality of the methods. Compile the program using the modified vector class.Submit the files vector.h and TestVector.cpp along with the test results.

Vector.h

#ifndef VECTOR_H #define VECTOR_H #include #include #include #include "dsexceptions.h" template class Vector { public: explicit Vector( int initSize = 0 ) : theSize{ initSize }, theCapacity{ initSize + SPARE_CAPACITY } { objects = new Object[ theCapacity ]; } Vector( const Vector & rhs ) : theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ nullptr } { objects = new Object[ theCapacity ]; for( int k = 0; k < theSize; ++k ) objects[ k ] = rhs.objects[ k ]; } Vector & operator= ( const Vector & rhs ) { Vector copy = rhs; std::swap( *this, copy ); return *this; } ~Vector( ) { delete [ ] objects; } Vector( Vector && rhs ) : theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ rhs.objects } { rhs.objects = nullptr; rhs.theSize = 0; rhs.theCapacity = 0; } Vector & operator= ( Vector && rhs ) { std::swap( theSize, rhs.theSize ); std::swap( theCapacity, rhs.theCapacity ); std::swap( objects, rhs.objects ); return *this; } bool empty( ) const { return size( ) == 0; } int size( ) const { return theSize; } int capacity( ) const { return theCapacity; } Object & operator[]( int index ) { #ifndef NO_CHECK if( index < 0 || index >= size( ) ) throw ArrayIndexOutOfBoundsException{ }; #endif return objects[ index ]; } const Object & operator[]( int index ) const { #ifndef NO_CHECK if( index < 0 || index >= size( ) ) throw ArrayIndexOutOfBoundsException{ }; #endif return objects[ index ]; } void resize( int newSize ) { if( newSize > theCapacity ) reserve( newSize * 2 ); theSize = newSize; } void reserve( int newCapacity ) { if( newCapacity < theSize ) return;

Object *newArray = new Object[ newCapacity ]; for( int k = 0; k < theSize; ++k ) newArray[ k ] = std::move( objects[ k ] );

theCapacity = newCapacity; std::swap( objects, newArray ); delete [ ] newArray; } // Stacky stuff void push_back( const Object & x ) { if( theSize == theCapacity ) reserve( 2 * theCapacity + 1 ); objects[ theSize++ ] = x; } // Stacky stuff void push_back( Object && x ) { if( theSize == theCapacity ) reserve( 2 * theCapacity + 1 ); objects[ theSize++ ] = std::move( x ); } void pop_back( ) { if( empty( ) ) throw UnderflowException{ }; --theSize; } const Object & back ( ) const { if( empty( ) ) throw UnderflowException{ }; return objects[ theSize - 1 ]; } // Iterator stuff: not bounds checked typedef Object * iterator; typedef const Object * const_iterator; iterator begin( ) { return &objects[ 0 ]; } const_iterator begin( ) const { return &objects[ 0 ]; } iterator end( ) { return &objects[ size( ) ]; } const_iterator end( ) const { return &objects[ size( ) ]; } static const int SPARE_CAPACITY = 2; private: int theSize; int theCapacity; Object * objects; }; #endif

dsexceptions.h

#ifndef _DSEXCEPTIONS_H_

#define _DSEXCEPTIONS_H_

class Underflow { };

class Overflow { };

class OutOfMemory { };

class BadIterator { };

#endif

TestVector.cpp

#include "Vector.h" #include #include using namespace std;

void print( const Vector> arr ) { int N = arr.size( ); for( int i = 0; i < N; ++i ) { cout << "arr[" << i << "]:"; for( int j = 0; j < arr[ i ].size( ); ++j ) cout << " " << arr[ i ][ j ]; cout << endl; } } class CompareVector { public: bool operator() ( const Vector & lhs, const Vector & rhs ) const { return lhs.size( ) < rhs.size( ); } }; int main( ) { const int N = 20; Vector> arr( N ); Vector v; for( int i = N - 1; i > 0; --i ) { v.push_back( i ); arr[ i ] = v; } print( arr ); clock_t start = clock( ); std::sort( begin( arr ), end( arr ), CompareVector{ } ); clock_t end = clock( ); cout << "Sorting time: " << ( end - start ) << endl; print( arr ); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions