Question: vector.h: #ifndef VECTOR_H #define VECTOR_H #include #include #include template class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ), theCapacity( initsize
vector.h:
#ifndef VECTOR_H
#define VECTOR_H
#include
#include
#include
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 < theSize; ++k)
objects[ k ] = rhs.objects[ k ];
}
Vector & operator = ( const Vector & rhs )
{
Vector copy(rhs);
std::swap( *this, copy );
return *this;
}
~Vector() { delete [] objects; }
bool empty() const { return size() == 0; }
int size() const { return theSize; }
int capacity() const { return theCapacity; }
T & operator[]( int index )
{
assert(index >= 0 && index < theSize);
return objects[ index ];
}
void resize( int newSize )
{
if( newSize > theCapacity )
reserve( newSize * 2 );
theSize = newSize;
}
void reserve( int newCapacity )
{
if( newCapacity < theSize )
return;
T *newArray = new T[ newCapacity ];
for( int k = 0; k < theSize; ++k )
newArray[ k ] = std::move(objects[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 pop_back( )
{
assert(!empty());
--theSize;
}
const T & back () const
{
assert(!empty());
return objects[ theSize - 1 ];
}
const T & front() const
{
assert(!empty());
return objects[0];
}
void erase ( int k, int* it )
{
assert(!empty());
for(int i=k; i { it[i]=it[i+1]; } } void insert ( int k, T x ) { } static const int SPARE_CAPACITY = 2; private: int theSize; int theCapacity; T * objects; }; #endif my main.cpp: #include #include "Vector.h" #include using namespace std; int max_subseq_sum_alg4 ( const vector { int maxSum = 0; int thisSum = 0; for (int i = 0; i < vec1.size(); i++) { thisSum += vec1[i]; if (thisSum > maxSum) maxSum = thisSum; else if (thisSum < 0) thisSum = 0; } return maxSum; } void print ( const vector { for( int i = 0; i < vec1.size(); i++) { cout << vec1.at(i) << " , "; } } int main() { vector int next; int yourVectorElements; cout << "what is the size of your vector?"; cin >> yourVectorElements; cout << endl; for (int i=0; i<= yourVectorElements-1; i++) { cout << "interger: "; cin >> next; cout << endl; vec1.push_back(next); } cout << endl; cout << max_subseq_sum_alg4(vec1); //cout << print(vec1); vec1.erase(k * 2); return 0; } need to check the implementaion of 1. the void print funtion which should print the vector. 2. void erase(int k){} removes element at index k. 3. void insert(int k, T x){} inserts the new element x at index k by moving all other elements to the right after insertion at index k.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
