Question
I need help on exercise 1 on building my vector, I will provide my code below but can anyone help me improve my code based
I need help on exercise 1 on building my vector, I will provide my code below but can anyone help me improve my code based on exercise 1?
#ifndef VECTOR_H_INCLUDED #define VECTOR_H_INCLUDED
#include
template
class Vector
{ public: explicit Vector(int initSize = 0): theSize{init Size}, theCapacity{initSize + SPARE_CAPACITY} { data = newdata[theCapacity]; }
Vector( const Vector & rhs) : theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, data{nullptr} { data = new data [theCapacity]; for (int k = 0; k
Vector & operator= (const Vector & rhs) { Vector copy = rhs; std::swap(*this, copy); return *this; }
~Vector() { delete[ ] data; }
Vector(Vector && rhs) : theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, data{rhs.data} { rhs.data= nullptr; rhs.theSize= 0; rhs.theCapacity= 0; }
Vector & operator= (Vector && rhs) { std::swap( theSize, rhs.theSize); std::swap( theCapacity, rhs.theCapacity); std::swap(data, rhs.data);
return *this; }
void resize(int newSize) { if(newSize > theCapacity) reserve(newSize * 2); theSize= newSize; }
void reserve( int newCapacity) { if(newCapacity
data *newArray = new Object[newCapacity]; for (int k = 0; k
theCapacity = newCapacity; std::swap(objects, newArray); delete[] newArray;
}
data & operator[](int index) { return size() == 0; } const data & operator [](int index) const { return data[index]; }
bool empty() const { return size() == 0; }
int size() const { return theSize; }
int capacity() const { return theCapacity; }
void push_back(const data & x) { if(theSize == theCapacity) reserve(2 * theCapacity + 1); data[ theSize++ ]= x; }
void push_back(data && x) { if(theSize == theCapacity) reserve(2 * theCapacity + 1); data[theSize++] = std::move(x); }
void pop_back() { --theSize; }
const data & back () const { return data[theSize -1]; }
typedef data * iterator; typedef const data * const_iterator;
iterator begin() { return &objects [0]; }
const_interator begin() const { return &data[0]; }
iterator end() { return &data[size()]; } const_interator end() const { return &data[size()]; }
static const int SPARE_CAPACITY = 16;
private: int theSize; int theCapacity; T*data;
};
CSE 2020 LABORATORY -- Week 2 - Spring 2021 Instructor: Kerstin Voigt This lab will have you do the hard labor of reproducing a C++ implementation of the Vector ADT. The implementation closely follows the one in Chapter 2 of Weiss, Data Structures and Algorithm Analysis in C++. However, the instructor made a few modifications: Vector
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