Question: Lab 2 Code: / File: Vector.h #ifndef VECTOR_H #define VECTOR_H #include #include #include // KV: instead of exceptions template class Vector { public: Vector( int

 Lab 2 Code: / File: Vector.h #ifndef VECTOR_H #define VECTOR_H #include#include #include // KV: instead of exceptions template class Vector { public:

Lab 2 Code:

/ File: Vector.h #ifndef VECTOR_H #define VECTOR_H

#include #include #include // KV: instead of exceptions

template class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ),

theCapacity( initsize + SPARE_CAPACITY ) { objects = new T[ theCapacity ]; }

Vector( const Vector & rhs ) : theSize( rhs.theSize ),

theCapacity( rhs.theCapacity ), objects( 0 ) {

objects = new T[ theCapacity ]; for( int k = 0; k

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

Vector & operator= ( const Vector & rhs ) { //Vector copy = rhs; Vector copy(rhs); // alternative; KV 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; }

T & operator[]( int index ) {

assert(index >= 0 && index

}

const T & operator[]( int index ) const {

assert(index >= 0 && index

}

void resize( int newSize ) {

if( newSize > theCapacity ) reserve( newSize * 2 );

theSize = newSize; }

void reserve( int newCapacity ) {

if( newCapacity

T *newArray = new T[ newCapacity ];

for( int k = 0; k

theCapacity = newCapacity; std::swap( objects, newArray ); delete [ ] newArray;

}

void push_back( const T & x ) {

if( theSize == theCapacity ) reserve( 2 * theCapacity + 1 );

objects[ theSize++ ] = x; }

void push_back( T && x ) {

if( theSize == theCapacity ) reserve( 2 * theCapacity + 1 );

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

void pop_back( ) {

assert(!empty()); --theSize;

}

const T & back ( ) const {

assert(!empty()); return objects[ theSize - 1 ];

}

const T & front() const {

assert(!empty()); return objects[0];

}

static const int SPARE_CAPACITY = 2;

private: int theSize; int theCapacity;

T * objects; };

#endif

Test your Vector implementation with the following int main() function:

// File: VectorMain.cpp #include #include "Vector.h" using namespace std;

void print_vector(Vector v) {

for (int i = 1; i

cout

int main() {

Vector v1; for (int i = 1; i

v1.push_back(i);

cout

Vector v2(v1);

cout

Vector v3 = v2;

cout

Vector v4(static_cast&&>(v3));

cout

Vectorv5 = static_cast&&>(v2);

cout

return 0; }

Complete the ADT Vector implementation from Lab2 and add the following two functions to the Vector class 1. Member function void erase (int k) which removes the element at index k. 2. Member function void insert (int k, T x)f.. which inserts the new element x at index k. (The element that is at index k prior to insertion, as well as all other elements at higher-numbered indices are to move one position to the right.) Details of the processes behind both functions are depicted below Erase: Erase the element at index 3 in vector [2][4][6][?[10] [12][14] [16] Step-by-step 0 1 2 3 4 5 6 7 [2][4](6][81[101[12][14][16] [2]I4][6][101[10][12][141[16] .[2][4][6][10][121[121[141[16] [2][4][6][10][12][14][141[16] [2][4][6][101[12][14][16][16] 214)6101211416161 . [2]I4][6][101[12][14][16] DONE! Look at the example and realize the kinds of shifts (really, copies") that will have the desired effect. Also notice the duplicate element at the end and how it is removed in order to produce the final result. Your function should also handle the special case where the removal index is the index of the element at the highest index what will be the simplest way to accomplish this element's removal? (Hint: this function already exists). Complete the ADT Vector implementation from Lab2 and add the following two functions to the Vector class 1. Member function void erase (int k) which removes the element at index k. 2. Member function void insert (int k, T x)f.. which inserts the new element x at index k. (The element that is at index k prior to insertion, as well as all other elements at higher-numbered indices are to move one position to the right.) Details of the processes behind both functions are depicted below Erase: Erase the element at index 3 in vector [2][4][6][?[10] [12][14] [16] Step-by-step 0 1 2 3 4 5 6 7 [2][4](6][81[101[12][14][16] [2]I4][6][101[10][12][141[16] .[2][4][6][10][121[121[141[16] [2][4][6][10][12][14][141[16] [2][4][6][101[12][14][16][16] 214)6101211416161 . [2]I4][6][101[12][14][16] DONE! Look at the example and realize the kinds of shifts (really, copies") that will have the desired effect. Also notice the duplicate element at the end and how it is removed in order to produce the final result. Your function should also handle the special case where the removal index is the index of the element at the highest index what will be the simplest way to accomplish this element's removal? (Hint: this function already exists)

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!