Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#ifndef VECTOR _ H #define VECTOR _ H #include #include #include template class Vector { public: explicit Vector ( int initSize = 0 ) :
#ifndef VECTORH
#define VECTORH
#include
#include
#include
template
class Vector
public:
explicit Vector int initSize
: theSize initSize theCapacity initSize SPARECAPACITY
objects new Object theCapacity ;
Vector const Vector & rhs
: theSize rhstheSize theCapacity rhstheCapacity objects nullptr
objects new Object theCapacity ;
for int k ; k theSize; k
objects k rhsobjects k ;
Vector & operator const Vector & rhs
Vector copy rhs;
std::swapthis copy ;
return this;
~Vector
delete objects;
Vector Vector && rhs
: theSize rhstheSize theCapacity rhstheCapacity objects rhsobjects
rhsobjects nullptr;
rhstheSize ;
rhstheCapacity ;
Vector & operator Vector && rhs
std::swap theSize, rhstheSize ;
std::swap theCapacity, rhstheCapacity ;
std::swap objects, rhsobjects ;
return this;
bool empty const
return size;
int size const
return theSize;
int capacity const
return theCapacity;
Object & operator int index
if index index size
return;
return objects index ;
const Object & operator int index const
if index index size
return;
return objects index ;
void resize int newSize
if newSize theCapacity
reserve newSize ;
theSize newSize;
void reserve int newCapacity
if newCapacity theSize
return;
Object newArray new Object newCapacity ;
for int k ; k theSize; k
newArray k std::move objects k ;
theCapacity newCapacity;
std::swap objects, newArray ;
delete newArray;
Stacky stuff
void pushback const Object & x
if theSize theCapacity
reserve theCapacity ;
objects theSize x;
Stacky stuff
void pushback Object && x
if theSize theCapacity
reserve theCapacity ;
objects theSize std::move x ;
void popback
if empty
return;
theSize;
const Object & back const
if empty
return;
return objects theSize ;
Print functionality
void print std::ostream & os std::cout const
os "Vector: ;
for int i ; i theSize; i
os objects i ;
os std::endl;
Iterator stuff: no bounds checked
typedef Object iterator;
typedef const Object constiterator;
iterator begin
return &objects;
constiterator begin const
return &objects;
iterator end
return &objects size;
constiterator end const
return &objects size;
static const int SPARECAPACITY ;
private:
int theSize;
int theCapacity;
Object objects;
;
#endif
For the class Vector, write down the implementation of the method shuffle. On a vector v vshuffle
swaps v and v v and v 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 vshuffle 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started