Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

#ifndef VECTOR _ H #define VECTOR _ H #include #include #include template class Vector { public: explicit Vector ( int initSize = 0 ) :

#ifndef VECTOR_H
#define VECTOR_H
#include
#include
#include
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 )
{
if( index <0|| index >= size())
return;
return objects[ index ];
}
const Object & operator[]( int index ) const
{
if( index <0|| index >= size())
return;
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())
return;
--theSize;
}
const Object & back () const
{
if( empty())
return;
return objects[ theSize -1];
}
// Print functionality
void print( std::ostream & os = std::cout ) const
{
os << "Vector: ";
for( int i =0; i < theSize; ++i )
os << objects[ i ]<<"";
os << std::endl;
}
// Iterator stuff: no 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
For the class Vector, write down the implementation of the method shuffle. On a vector v, v.shuffle
swaps v[0] and v[1], v[2] and v[3],.... If the size of v is odd, the method does not affect the last element. For
example, if v ={a, b, c, d, e, f, g }, then v.shuffle() will return {b, a, d, c, f, e, g }. Note: A slightly modified version of the implementation of the class Vector is provided in the file Vector.h that
accompanies this assignment. You can build upon that.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions