In C++, you are implementing the vector class in the VectorStub zip. You need to implement all of the methods, and that's it. You cannot add any new properties. Your solutions must conform to the Big O notations next to each method.
A few things to remember about templates:
The hard part of this is the memory management, so take special care with Reserve, the constructors, and the destructor.
Vector.h
// 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 that
// doesn't come up for a few weeks.
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 I'm just giving it to ya. 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. We'll
change its prototype next week
}
void Insert(int tWhere, const T& tWhat)//
O(n)
{
// Keep an eye on this one. We'll
change its prototype next week
}
void Clear()// O(1)
{
}
int Size()// O(1)
{
return 0;
}
void Reserve(int tCount)// O(n)
{
}
int Capacity()// O(1)
{
return 0;
}
};
template <typename T>
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)
{
mCapacity = 0;
mSize = 0;
mData = nullptr;
}
Vector(const Vector& tOther) : Vector()// O(n)
{
mCapacity = tOther.mCapacity;
mSize = tOther.mSize;
mData = new T[mCapacity];
for(int i = 0; i < mSize; i++)
mData[i] = tOther.mData[i];
}
Vector &operator =(const Vector& tRHS)// O(n)
{
if(this != &tRHS){
delete mData;
mCapacity = tRHS.mCapacity;
mSize = tRHS.mSize;
mData = new T[mCapacity];
for(int i = 0; i < mSize; i++)
mData[i] = tRHS.mData[i];
}
return *this; // This line is weird so I'm just giving it to ya.
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.
Reserve(mSize + 1);
mData[mSize++] = tItem;
}
void PopBack()// O(1)
{
if(mSize > 0)
mSize--;
}
void PushFront(const T &tItem)// O(n)
{
Reserve(mSize + 1);
for(int i = mSize-1; i >= 0; i--)
mData[i+1] = mData[i];
mSize++;
}
void PopFront()// O(n)
{
if(mSize > 0){
for(int i = 1; i < mSize; i++)
mData[i-1] = mData[i];
mSize--;
}
}
T& At(int tWhere)// O(1)
{
if(tWhere < 0 || tWhere >= mSize)
return mUndefined;
else
return mData[tWhere];
}
void Erase(int tWhere)// O(n)
{
// Keep an eye on this one. We'll change its prototype next
week
if(tWhere >= 0 && tWhere < mSize){
for(int i = tWhere+1; i < mSize; i++)
mData[i-1] = mData[i];
mSize--;
}
}
void Insert(int tWhere, const T& tWhat)// O(n)
{
// Keep an eye on this one. We'll change its prototype next
week
if(tWhere >= 0 && tWhere < mSize){
Reserve(mSize+1);
for(int i = mSize-1; i>= tWhere; i--)
mData[i+1] = mData[i];
mData[tWhere] = tWhat;
mSize++;
}
}
void Clear()// O(1)
{
mSize = 0;
mCapacity = 0;
delete mData;
mData = nullptr;
}
int Size()// O(1)
{
return mSize;
}
void Reserve(int tCount)// O(n)
{
if(mCapacity < tCount){
mCapacity = tCount;
T* temp = new T[mCapacity];
for(int i = 0; i < mSize; i++)
temp[i] = mData[i];
if(mData != nullptr)
delete mData;
mData = temp;
}
}
int Capacity()// O(1)
{
return mCapacity;
}
};
In C++, you are implementing the vector class in the VectorStub zip. You need to implement...
Implementing the vector class in the following code (It's C++, not Java). You just need to implement all these methods: PushFront, PopFront, At, Erase, Insert, Clear, Reserve, Copy, Assign, and Destroy, 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...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...
In this assignment, you will implement a sort method on
singly-linked and doubly-linked lists.
Implement the following sort member function on a singly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
Implement the following sort member function on a doubly-linked
list:
void
sort(bool(*comp)(const
T &, const
T &) = defaultCompare);
The sort(…) methods take as a parameter a comparator function,
having a default assignment of defaultCompare, a
static function defined as follows:
template <typename T>
static bool defaultCompare(const...
Review the Stack implementation with Vector, and
implement/answer the following methods.
Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...
C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...
vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector { public: Vector( int initsize = 0 ) : theSize( initsize ), theCapacity( initsize + SPARE_CAPACITY ) { objects = new T[ theCapacity ]; } Vector( const Vector & rhs ) : theSize( rhs.theSize), theCapacity( rhs.theCapacity ), objects( 0 ) { objects = new T[ theCapacity ]; for( int k = 0; k < theSize; ++k) objects[ k ] = rhs.objects[ k...
I'm just not sure how to tackle all the template class this header wants me to write. I got this far into making the template for public and private and would like help on the template functions. Thank you! This is in C++ by the way. #include <iostream> #include <cassert> using namespace std; #ifndef ARRAYLIST_H #define ARRAYLIST_H template<typename T> class arrayList { public: arrayList(); //Constructor with default parameter. //Sets maxSize = 100 and length = 0 if no parameter...
IN C++ Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on...
In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the code in main to create and sort the array of pointers. The places to make modifications are indicated by TODO: comments. You should not have to make modifications anywhere else. 3- what's big O and runtime for 100000 items. #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <cstdlib> #include <cassert> using namespace std; // Set this to false to skip the...
I need to complete the C++ program for hotel reservation. In the main cpp you have to display the hotel information from the hotel.txt based on customer preferences(which can be taken from the User.txt) For example, if the customer's budget is 200$, then only the hotels with prices below that amount should be displayed. Then the user has an option to choose which one to reserve for the particular time(which also can be found in the user.txt) The last two...