ng ifesi vector is by the following so You need to write the complete C 1\"\"e\"
ID: 3571251 • Letter: N
Question
ng ifesi vector is by the following so You need to write the complete C 1""e" a questions. define change its size. The following gives vector class declaration VECTOR H the H include using namespace std; class Vector public Vector (int Constructor Vector news constructor destructor void load Data (string filename) load data from a text fine void store (int into data on index) store the first parameter val location the index-th location value from data a the index-th valueAt (int index): get the int Max int Max find the max value in data using a loop value r (const int data 1, int first. int last); find the max int in data using recursion sum using a loop int Sum calculate the of data sum Sum r (const int datat 1, int first, calculate the of data int last); using recursion void Print print the list of numbers in data in order using a loop void Print r (const int datau, int first, int last) print the list of numbers in data in order using recursion int *data; store a list of data. int size store the size of input data array int capacity store the capacity of data #endifExplanation / Answer
template <class T>
class Vector2{
public:
typedef T* Iterator;
Vector2();
Vector2(unsigned int size);
Vector2(unsigned int size, const T & initial);
Vector2(const Vector<T>& v);
~Vector2();
unsigned int capacity() const;
unsigned int size() const;
bool empty() const;
Iterator begin();
Iterator end();
T& front();
T& back();
void push_back(const T& value);
void pop_back();
void reserve(unsigned int capacity);
void resize(unsigned int size);
T & operator[](unsigned int index);
Vector<T> & operator = (const Vector<T> &);
void clear();
private:
unsigned int _size;
unsigned int _capacity;
unsigned int Log;
T* buffer;
};
template<class T>
Vector<T>::Vector2() {
_capacity = 0;
_size = 0;
buffer = 0;
Log = 0;
}
template<class T>
Vector<T>::Vector2(const Vector<T> & v) {
_size = v._size;
Log = v.Log;
_capacity = v._capacity;
buffer = new T[_size];
for (unsigned int i = 0; i < _size; i++)
buffer[i] = v.buffer[i];
}
template<class T>
Vector<T>::Vector2(unsigned int size) {
_size = size;
Log = ceil(log((double) size) / log(2.0));
_capacity = 1 << Log;
buffer = new T[_capacity];
}
template <class T>
bool Vector<T>:: empty() const {
return _size == 0;
}
template<class T>
Vector<T>::Vector2(unsigned int size, const T& initial) {
_size = size;
Log = ceil(log((double) size) / log(2.0));
_capacity = 1 << Log;
buffer = new T [_capacity];
for (unsigned int i = 0; i < size; i++)
buffer[i] = initial;
}
template<class T>
Vector<T>& Vector<T>::operator = (const Vector<T> & v) {
delete[] buffer;
_size = v._size;
Log = v.Log;
_capacity = v._capacity;
buffer = new T [_capacity];
for (unsigned int i = 0; i < _size; i++)
buffer[i] = v.buffer[i];
return *this;
}
template<class T>
typename Vector<T>::Iterator Vector<T>::begin() {
return buffer;
}
template<class T>
typename Vector<T>::Iterator Vector<T>::end() {
return buffer + size();
}
template<class T>
T& Vector<T>::front() {
return buffer[0];
}
template<class T>
T& Vector<T>::back() {
return buffer[_size - 1];
}
template<class T>
void Vector<T>::push_back(const T & v) {
if (_size >= _capacity) {
reserve(1 << Log);
Log++;
}
buffer [_size++] = v;
}
template<class T>
void Vector<T>::pop_back() {
_size--;
}
template<class T>
void Vector<T>::reserve(unsigned int capacity) {
T * newBuffer = new T[capacity];
for (unsigned int i = 0; i < _size; i++)
newBuffer[i] = buffer[i];
_capacity = capacity;
delete[] buffer;
buffer = newBuffer;
}
template<class T>
unsigned int Vector<T>::size() const {
return _size;
}
template<class T>
void Vector<T>::resize(unsigned int size) {
Log = ceil(log((double) size) / log(2.0));
reserve(1 << Log);
_size = size;
}
template<class T>
T& Vector<T>::operator[](unsigned int index) {
return buffer[index];
}
template<class T>
unsigned int Vector<T>::capacity()const {
return _capacity;
}
template<class T>
Vector<T>::~Vector2() {
delete[] buffer;
}
template <class T>
void Vector<T>::clear() {
_capacity = 0;
_size = 0;
buffer = 0;
Log = 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.