Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Vector.h #pragma once // I am going to match the names that STL uses so you don't get confused in the real world. I'm skipping

image text in transcribed

Vector.h

#pragma once

// 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.

// Making this static for maximum const correctness

static T sUndefined;// 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.

// I like the static solution because there is a single "bad T" in

the universe, and I can put a breakpoint on it changing.

public:

Vector()// O(1)

{

mSize = 0;

mCapacity = 15;

mData = new T[mCapacity];

}

Vector(const Vector& tOther) : Vector()// O(n)

{

// Same capacity

Reserve(tOther.Capacity());

// Same Data

for (int i = 0; i

are equal now

mData[i] = tOther.At(i);

mSize = tOther.Size();

}

Vector &operator =(const Vector& tRHS)// O(n)

{

// Same capacity

Reserve(tRHS.Capacity());

// Same Data

for (int i = 0; i

are equal now

mData[i] = tRHS.At(i);

mSize = tRHS.Size();

return *this; // This line is weird so I'm just giving it

to ya. It's just the definition of an =

}

~Vector()

{

delete[] mData;// You new it, you delete it

}

void PushBack(const T &tItem)// O(1)

{

// We take a const reference, but we _copy_ it in to our

personal array.

if (mSize == mCapacity)// If I'm full...

Reserve(2 * mCapacity);// get more memory

mData[mSize] = tItem;// Put the data in array

mSize++;// Keep track of how many used

}

void PopBack()// O(1)

{

if (mSize > 0)

mSize--;

}

void PushFront(const T &tItem)// O(n)

{

// Check for capacity first

if (mSize == mCapacity)// If I'm full...

Reserve(2 * mCapacity);// get more memory

for (int i = mSize - 1; i >= 0; i--)

{

// "Shift" data one right.

// We don't want to overwrite anything so we

better start on the right

mData[i + 1] = mData[i];

}

// Add new data to front

mData[0] = tItem;

// Update size variable

mSize++;

}

void PopFront()// O(n)

{

// Check for empty

if (mSize == 0)

return;

// Shift data left

for (int i = 0; i

mData[i] = mData[i + 1];

mSize--;

}

T& At(int tWhere) const // O(1)

{

// BUT first check out of bounds

// Access the array like normal

if( tWhere = mSize )

return sUndefined;

return mData[tWhere];

}

void Clear()// O(1)

{

mSize = 0;

}

int Size() const// O(1)

{

return mSize;

}

void Reserve(int tCount)// O(n)

{

int tOldSize = mSize;

if( tCount

tOldSize = tCount;// If we are shrinking

// Reserve more memory

T* tData = new T[tCount];

// Preserve old values

for (int i = 0; i

tData[i] = mData[i];

// Delete old array

delete[] mData;

// Connect new array

mData = tData;

mCapacity = tCount;

mSize = tOldSize;

}

int Capacity() const// O(1)

{

return mCapacity;

}

class Iterator

{

public:

int mCurrentIndex = 0;

Vector *mMyVectorAsPointer;

public:

Iterator(Vector *tVector, int tLocation)

{

}

T& GetData()

{

// mMyVectorAsPointer is needed to get to

mData or At.

// Remember how you implement the private

stuff is up to you as long as you obey the Big O.

}

void Next()// As in "Move to the next item please".

{

}

bool IsEqual(const Iterator &rhs)

{

// We're equal if all of our properties are

equal.

}

};

Iterator Insert(Iterator tWhere, const T &tWhat) // These two use

iterators now

{

// Returns an iterator to help if you are changing a loop

while you are traversing it.

// The iterator is after the insert or erase point.

}

Iterator Erase(Iterator tWhere)

{

}

Iterator Begin()

{

// First good data

Iterator tAnswer(nullptr, 0);

return tAnswer;

}

Iterator End()

{

// First Bad data

Iterator tAnswer(nullptr, 0);

return tAnswer;

}

};

template

T Vector::sUndefined;

Go back to your Vector and add iterators. Use the contract in the List stub since in STL iterators all follow the same commands Once you add the iterator, you can fix Remove and Insert to use iterators instead of indexes, like they are supposed to. And add Begin and End of course. Go back to your Vector and add iterators. Use the contract in the List stub since in STL iterators all follow the same commands Once you add the iterator, you can fix Remove and Insert to use iterators instead of indexes, like they are supposed to. And add Begin and End of course

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_2

Step: 3

blur-text-image_3

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

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions

Question

3. Define the attributions we use to explain behavior

Answered: 1 week ago