Using the code given below, implement the functions in the vector class in a sep
ID: 3630942 • Letter: U
Question
Using the code given below, implement the functions in the vector class in a separate files called “my_vector.cpp”. Provided are two test files, main.cpp and test_vector.cpp, both of which validate the functionality of your vectors.main.cpp has some very simple tests in it so you can start out testing. I’d suggest making sure you implement enough functionality in your my_vector.cpp file to appease the basic main.cpp and then add methods and tests from test_vector.cpp as you progress. You will be done when test_vector.cpp compiles and runs with no output at all.
Listing: Makefile
main: my_vector.h my_vector.cpp main.cpp
g++ main.cpp -ggdb -o main
test_vector: my_vector.h my_vector.cpp test_vector.cpp
g++ -ggdb test_vector.cpp -o test_vector
Listing: main.cpp
// main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include "my_vector.h"
using namespace std;
int main(char* args[])
{
// Swap these to test against the STL vector
//#define VECTOR vector
#define VECTOR my_vector
VECTOR<int> v;
v.reserve(2);
assert(v.capacity() == 2);
VECTOR<string> v1(2);
assert(v1.capacity() == 2);
assert(v1.size() == 2);
assert(v1[0] == "");
assert(v1[1] == "");
}
Listing: test_vector.cpp
// test_my_vector.cpp
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include "my_vector.h"
using namespace std;
int main(char* args[])
{
//#define VECTOR vector
#define VECTOR my_vector
VECTOR<int> v;
v.reserve(2);
assert(v.capacity() == 2);
VECTOR<string> v1(2);
assert(v1.capacity() == 2);
assert(v1.size() == 2);
assert(v1[0] == "");
assert(v1[1] == "");
v1[0] = "hi";
assert(v1[0] == "hi");
VECTOR<int> v2(2, 7);
assert(v2[1] == 7);
VECTOR<string> 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<string> 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<int> 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);
assert(v6[1] == 101);
cout << "OK ";
cin.get();
}
Listing: my_vector.h
#ifndef MY_VECTOR_H
#define MY_VECTOR_H
// my_vector.h
using namespace std;
template <class T>
class my_vector
{
public:
// Constructors / Destructor
my_vector();
// Constructor with initial size
my_vector(unsigned int size);
// Constructor with initial size -- set all initial spaces to initial param
my_vector(unsigned int size, const T & initial);
// Copy constructor
my_vector(const my_vector & v);
~my_vector();
// Return capacity of vector (in elements)
int capacity() const;
// Return the number of elements in the vector
unsigned int size() const;
// Return true if vector has no elements
bool empty() const;
// Add a new element to the end of the backing array
// Don't forget to resize if needed!
void push_back(const T & value);
// Remove the last element (don't return it)
void pop_back();
// Adjust capacity (make more space as needed)
void reserve(unsigned int new_capacity);
// Resize vector (even if that means making it smaller)
void resize(unsigned int new_size); // adjust size
/* Overloaded operators */
// Return a *reference* to numbered element
T & operator [ ](unsigned int index) const;
private:
unsigned int my_size;
unsigned int my_capacity;
T * buffer;
};
/* put all your code in my_vector.cpp */
#include "my_vector.cpp"
#endif
Explanation / Answer
template <class T>
my_vector<T>::my_vector()
{
my_size = 0;
my_capacity = 1;
buffer = new T[my_capacity];
}
template <class T>
my_vector<T>::my_vector(unsigned int size)
{
my_size = size;
my_capacity = my_size;
buffer = new T[my_capacity];
}
template <class T>
my_vector<T>::my_vector(unsigned int size, const T & initial)
{
my_size = size;
my_capacity = my_size;
buffer = new T [my_capacity];
for (int i = 0; i < my_capacity; i++ )
buffer[i] = *initial;
}
template <class T>
my_vector<T>::my_vector(const my_vector & v)
{
my_size = size;
my_capacity = my_size;
buffer = new T [my_capacity];
for (int i = 0; i < my_capacity; i++)
buffer[i] = v;
}
template <class T>
my_vector<T>::~my_vector()
{
delete [] buffer;
}
template <class T>
int my_vector<T>::capacity() const
{
return my_capacity;
}
template <class T>
unsigned int my_vector<T>::size() const
{
return my_size;
}
template <class T>
bool my_vector<T>::empty() const
{
return my_size();
}
template <class T>
void my_vector<T>::push_back(const T & value)
{
if (my_size>my_capacity)
push_back(& value);
}
template <class T>
void my_vector<T>::pop_back()
{
while (!buffer.empty())
{
buffer.pop_back();
}
}
template <class T>
void my_vector<T>::reserve(unsigned int new_capacity)
{
T* temp = new T [new_capacity];
for (int i = 0; i < my_capacity; i++)
temp[i] = buffer[i];
delete buffer;
buffer = temp;
my_capacity = new_capacity;
}
template <class T>
void my_vector<T>::resize(unsigned int new_size)
{
if (new_size > my_size)
{
my_size = new_size;
reserve(new_size);
}
else
{
my_size = new_size;
}
}
template <class T>
T & my_vector<T>:: operator [ ](unsigned int index) const
{
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.