Question: Use c++. Thanks Vector.h file #ifndef VECTOR_H #define VECTOR_H //a collection of functions especially designed to be used on ranges of elements. #include //cout, cin,

Use c++. Thanks

Use c++. Thanks Vector.h file #ifndef VECTOR_H #define VECTOR_H //a collection of

Vector.h file

#ifndef VECTOR_H

#define VECTOR_H

//a collection of functions especially designed to be used on ranges of elements.

#include

//cout, cin, etc...

#include

//Exception classes

#include

//Userdefined exception classes

#include "dsexceptions.h"

//We are making our class generic with this line here.

template

class Vector{

public:

// Our vector constructor. We initialize its size to be the paramter plus 2.

explicit Vector( int initSize = 0 )

: theSize{ initSize }, theCapacity{ initSize + SPARE_CAPACITY }

{

//Dynamically allocate an array of theCapacity size

objects = new Object[ theCapacity ];

}

//Copy constructor

Vector( const Vector & rhs )

: theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ nullptr }

{

objects = new Object[ theCapacity ];

for( int k = 0; k

objects[ k ] = rhs.objects[ k ];

}

//Overloaded copy assignment operator

Vector & operator= ( const Vector & rhs )

{

Vector copy = rhs;

std::swap( *this, copy );

return *this;

}

//Class destructor

~Vector( )

{ delete [ ] objects; }

//Move Constructor

Vector( Vector && rhs )

: theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ rhs.objects }

{

rhs.objects = nullptr;

rhs.theSize = 0;

rhs.theCapacity = 0;

}

//Overloaded Move Assignment operator

Vector & operator= ( Vector && rhs )

{

std::swap( theSize, rhs.theSize );

std::swap( theCapacity, rhs.theCapacity );

std::swap( objects, rhs.objects );

return *this;

}

//Returns whether vector is empty or not

bool empty( ) const

{ return size( ) == 0; }

//returns size of the vector, i.e., how many elements are in it

int size( ) const

{ return theSize; }

//returns the capacity of the vector, i.e., how many elements it can store

int capacity( ) const

{ return theCapacity; }

//overload the [] operator. This particular example can check for bounds.

Object & operator[]( int index )

{

#ifndef NO_CHECK

if( index = size( ) )

throw ArrayIndexOutOfBoundsException{ };

#endif

return objects[ index ];

}

//Same as above but do not allow for modification of the accessed item in the array

const Object & operator[]( int index ) const

{

#ifndef NO_CHECK

if( index = size( ) )

throw ArrayIndexOutOfBoundsException{ };

#endif

return objects[ index ];

}

//Set's the array size, increasing capacity if needed.

void resize( int newSize )

{

if( newSize > theCapacity )

reserve( newSize * 2 );

theSize = newSize;

}

//Increases the vector's backing array's capacity.

void reserve( int newCapacity )

{

//Enough capacity. Do nothing.

if( newCapacity

return;

//Get another pointer to the objects array

Object *oldArray = objects;

//Let the objects pointer point to a new array with more capacity

objects = new Object[ newCapacity ];

//Copy from the "old" array to the new one.

for( int k = 0; k

objects[ k ] = oldArray[ k ];

//Update the capacity

theCapacity = newCapacity;

//No longer need the old array

delete [ ] oldArray;

}

// Stacky stuff. Add to the end of the vector's array

void push_back( const Object & x )

{

if( theSize == theCapacity )

reserve( 2 * theCapacity + 1 );

objects[ theSize++ ] = x;

}

// Stacky stuff. Same as above except for an rvalue.

void push_back( Object && x )

{

if( theSize == theCapacity )

reserve( 2 * theCapacity + 1 );

objects[ theSize++ ] = std::move( x );

}

// Decrease the vector's size. Doesn't really delete the item right away

void pop_back( )

{

if( empty( ) )

throw UnderflowException{ };

--theSize;

}

//Last element in the vector's array

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( ) ]; }

// Problem #3

iterator insert(iterator pos, const Object & x){

}

static const int SPARE_CAPACITY = 2;

private:

int theSize;

int theCapacity;

Object * objects;

};

#endif

3. For the Vector class available on Blackboard, add an iterator insert(iterator pos, const Object & ) method. Note to move the iterators (which are really just pointers here) around, you need to add to them sizeOf(Object). For example, pos += szeOf(Object); lets you examine the next item in the Object array

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!