Question: Implementing the vector class in the following code (It's C++, not Java). You just need to implement all the methods, please do not add any
Implementing the vector class in the following code (It's C++, not Java). You just need to implement all the methods, please do not add any new properties. You cannot use std:: functions, && (unless being used for "AND"), or pass by reference. Your solutions must conform to the Big O notations next to each method.
In the following code, I am going to match the names that STL uses so you don't get confused in the real world. I'm skipping operator [] for a specific reason, please don't ask about it.
template
class Vector
{
T* mData;
int mSize;
int mCapacity;// For testing purposes, initialize this to 15. Whenever you allocate new memory, double it.
T mUndefined;// Lots of STL functions say that doing something naughty gets "undefined behavior". It could throw, crash, make you eggs, or return nonsense.
// Return this undefined one if anybody ever tries to go out of bounds.
public:
Vector()// O(1)
{
mSize = 0;
mData = nullptr;
}
Vector(const Vector& tOther) : Vector()// O(n)
{
}
Vector &operator =(const Vector& tRHS)// O(n)
{
return *this; // This line is weird so the professor just giving it. It's just the definition of an =
}
void PushBack(const T &tItem)// O(1)
{
// We take a const reference, but we _copy_ it in to our personal array.
}
void PopBack()// O(1)
{
}
void PushFront(const T &tItem)// O(n)
{
}
void PopFront()// O(n)
{
}
T& At(int tWhere)// O(1)
{
return mUndefined;
}
void Erase(int tWhere)// O(n)
{
// Keep an eye on this one.
}
void Insert(int tWhere, const T& tWhat)// O(n)
{
// Keep an eye on this one, too.
}
void Clear()// O(1)
{
}
int Size()// O(1)
{
return 0;
}
void Reserve(int tCount)// O(n)
{
}
int Capacity()// O(1)
{
return 0;
}
};
// VectorStub.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include "pch.h"
#include
#include "Vector.h"
using namespace std;
int main()
{
// Remember, you have to test all template functions since if you don't call it then it gets deleted. Otherwise later when you'll call the one you forgot and it will crash.
Vector tTester;
tTester.PushBack(4);
tTester.PushBack(5);
tTester.PushBack(6);
tTester.PushBack(7);
tTester.PushBack(8);
tTester.PushBack(9);
tTester.PushFront(3);
tTester.PushFront(2);
tTester.PushFront(1);
tTester.PushFront(0);
cout << tTester.Size() << endl;// 10
cout << tTester.At(5) << endl;// 5
cout << tTester.At(0) << endl;// 0
cout << tTester.Capacity() << endl; // 15
tTester.PushBack(11);
tTester.PushBack(11);
tTester.PushBack(11);
tTester.PushBack(11);
tTester.PushBack(11);
tTester.PushBack(11);
tTester.PushBack(111);
tTester.PushBack(11);
cout << tTester.Capacity() << endl; // 30
tTester.Reserve(50);
cout << tTester.Capacity() << endl; // 50
tTester.PopFront();
cout << tTester.At(0) << endl;// 1
tTester.PopBack();
cout << tTester.At(tTester.Size() - 1) << endl;// 111
tTester.Erase(2);
cout << tTester.At(2) << endl;// 4
tTester.Insert(1, 99);
cout << tTester.At(2) << endl;// 2
tTester.Clear();
cout << tTester.Size() << endl;// 0
tTester.PushBack(0);
tTester.PushBack(1);
tTester.PushBack(2);
tTester.PushBack(3);
tTester.PushBack(4);
tTester.PushBack(5);
VectortCopy(tTester);
cout << tTester.Size() << endl;// 6
cout << tCopy.Size() << endl;// 6
tCopy.PopBack();
cout << tTester.Size() << endl;// 6
cout << tCopy.Size() << endl;// 5
Vector tAssign;
tAssign.PushBack(999);
tAssign = tTester; // This is a tricky thing, if you do an initialization (Vector A = B;) even though it has a = it runs the copy constructor instead.)
cout << tTester.Size() << endl;// 6
cout << tAssign.Size() << endl;// 6
tAssign.PopBack();
cout << tTester.Size() << endl;// 6
cout << tAssign.Size() << endl;// 5
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
