Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement class Vector that provides the vector functionality needed by the test code Vector_test.cpp given below. When you do this, you should not use C++

Implement class Vector that provides the vector functionality needed by the test code Vector_test.cpp given below. When you do this, you should not use C++ vector class.

Since Vector is a template container class, it should be implemented in one file: Vector.h. Complete Vector.h given below.

A few points regarding capacity versus size:

capacity: amount of memory allocated to the container in terms of number of elements. Number of bytes = capacity * length of T

size: number of elements in the container.

capacity is always >= size

capacity - size = number of elements that can be added without allocating more memory.

reserve(): increases capacity by allocating more memory.

resize(): could increase or decrease size. When increasing size, if necessary (when size > capacity) increase capacity to match size.

In your implementation when size = capacity and push_back() is called, allocate 5 more memory locations first (increase capacity by 5), then insert the new element (increase size by 1).

Take a look at capsize.cpp. Compile and run it and make sure you understand what it is doing.

#ifndef VECTOR_H #define VECTOR_H // Vector.h using namespace std; template  class Vector { public: typedef T * iterator; Vector(); Vector(unsigned int size); Vector(unsigned int size, const T & initial); Vector(const Vector & v); // copy constructor ~Vector(); unsigned int capacity() const; // return capacity of vector (in elements) unsigned int size() const; // return the number of elements in the vector bool empty() const; iterator begin(); // return an iterator pointing to the first element iterator end(); // return an iterator pointing to one past the last element T & front(); // return a reference to the first element T & back(); // return a reference to the last element void push_back(const T & value); // add a new element void pop_back(); // remove the last element void reserve(unsigned int capacity); // adjust capacity void resize(unsigned int size); // adjust size T & operator[](unsigned int index); // return reference to numbered element Vector operator=(const Vector &); private: unsigned int my_size; unsigned int my_capacity; T * buffer; }; // Two sample methods follow template  Vector::Vector() { my_size = 0; my_capacity = 0; buffer = 0; } template  // This line doesn't compile: Vector::iterator Vector::begin() // This line compiles: T * Vector::begin() // But the follwoing line is the better solution: typename Vector::iterator Vector::begin() { return buffer; } // Rest of your code goes here ... #endif 
#include  #include  #include  #include "Vector.h" using namespace std; int main() { Vector v; v.reserve(2); assert(v.capacity() == 2); Vector v1(2); assert(v1.capacity() == 2); assert(v1.size() == 2); assert(v1[0] == ""); assert(v1[1] == ""); v1[0] = "hi"; assert(v1[0] == "hi"); Vector v2(2, 7); assert(v2[1] == 7); Vector v10(v2); assert(v10[1] == 7); Vector v3(2, "hello"); assert(v3.size() == 2); assert(v3.capacity() == 2); assert(v3[0] == "hello"); assert(v3[1] == "hello"); v3.resize(1); assert(v3.size() == 1); assert(v3[0] == "hello"); Vector v4 = v3; assert(v4.size() == 1); assert(v4[0] == v3[0]); v3[0] = "test"; assert(v4[0] != v3[0]); // fails when assignment results in shallow copy assert(v4[0] == "hello"); v3.pop_back(); assert(v3.size() == 0); Vector v5(7, 9); Vector::iterator it = v5.begin(); while (it != v5.end()) { assert(*it == 9); ++it; } Vector v6; v6.push_back(100); assert(v6.size() == 1); assert(v6[0] == 100); v6.push_back(101); assert(v6.size() == 2); assert(v6[0] == 100); v6.push_back(101); cout << "SUCCESS "; } 

so far i have done the skeleton but need help filling in some classes

#ifndef VECTOR_H #define VECTOR_H #include "Vector.h"

// Vector.h

using namespace std;

template class Vector { public:

typedef T * iterator;

Vector(); Vector(unsigned int size); Vector(unsigned int size, const T & initial); Vector(const Vector & v); // copy constructor ~Vector();

unsigned int capacity() const; // return capacity of vector (in elements) unsigned int size() const; // return the number of elements in the vector bool empty() const;

iterator begin(); // return an iterator pointing to the first element iterator end(); // return an iterator pointing to one past the last element T & front(); // return a reference to the first element T & back(); // return a reference to the last element void push_back(const T & value); // add a new element void pop_back(); // remove the last element

unsigned int size() const; // return the number of elements in the vector bool empty() const;

iterator begin(); // return an iterator pointing to the first element iterator end(); // return an iterator pointing to one past the last element T & front(); // return a reference to the first element T & back(); // return a reference to the last element void push_back(const T & value); // add a new element void pop_back(); // remove the last element

void reserve(unsigned int capacity); // adjust capacity void resize(unsigned int size); // adjust size

T & operator[](unsigned int index); // return reference to numbered element Vector & operator=(const Vector &);

private: unsigned int my_size; unsigned int my_capacity; T * buffer; };

template Vector ::Vector() { my_size = 0; my_capacity= 0; buffer = 0; }

template Vector::Vector(unsigned int size) { my_size = my_capacity = size; buffer = new T[size]; for(int i=0; i=size; i++) buffer[i] = T; } template Vector::Vector(unsigned int size, const T & initial); { }

template Vector::Vector(const Vector & v { }

template Vector::~Vector() { if(buffer != NULL) delete[]buffer; }

template unsigned Vector::int capacity() const { }

template unsigned Vector:: int size() const { }

template bool Vector::empty() const { }

template Vector::iterator begin() { }

template Vector::iterator end() { }

template Vector::T & front() { }

template Vector::T & back() { }

template void Vector::push_back(const T & value) { }

template void Vector::pop_back() { }

template void Vector::reserve(unsigned int capacity) { }

template void Vector::resize(unsigned int size) { }

template Vector::T & operator[](unsigned int index) { }

template Vector & Vector::operator=(const Vector &); { my_size = v.my_size; my_capacity = v.my_capacity; }

template Vector & Vector::operator=(const Vector &); { my_size = v.my_size; my_capacity = v.my_capacity; if(buffer != NULL) delete[]buffer; buffer = new T[my_capacity]; for(int i = 0; i < my_size; i++) buffer [i] = v.buffer; return *this; }

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

More Books

Students also viewed these Databases questions

Question

Finding and scheduling appointments with new prospective clients.

Answered: 1 week ago