Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: Can you show the code you used to create? What if vector Didn\'t Exist? You

ID: 3859968 • Letter: C

Question

C++: Can you show the code you used to create?

What if vector Didn't Exist? Your task is to create a container that is similar to the vector class we use in STL. We will build a container called, MyVector from scratch - this class will not inherit from any existing container classes. Obviously, we will not implement all of a vector's functionality, but here are the things it must be able to do: 1. Your class will hold any number of elements of any type. 2. Add an element: call the a method push_back: will add an element to the end of a list. Call another method push_front: will add an element to the beginning of the list. 3. Remove an element from anywhere in the list - call the method removeAt 4. Pop (remove) an element from the end of the list 5. Insert an element at a specific location - call it insertAt 6. Use the [] operator (overloading) so we can loop through items using a for-loop 7. Implement iterators so we can do range-based for loops a. We will use both begin () and end () 8. Call a print method to "dump" the list's contents to the console 9. Call reset method to clear the entire list 10. Call an append method, passing in another vector to append to the end of the current vector. 11. Supply a sort method that will sort in either ascending or descending order. Be careful with this. The actual compare should be passed into your method as routine (remember), but you will have no idea about what you are actually comparing: therefore, you will need to require the caller of your method to supply the comparison routine to you. 12. Supply a reverse routine that will take the list of elements and simply reverse it. Easier Said Than Done? Well, maybe. However, we are going to draw from our knowledge and experience to put this container together. I will give you a few tips to get you started. First, there are a number of things to keep in mind: Templates: Since we want the ability to create and mange lists of any data type, we will be creating this class as a template class. If you are unsure of how to do this, refer to my previous lecture on templates: otherwise, you can lookup how to create a template class here: http: //www.cprogramming.com/tutorial/templates.html Linked Lists: While there are various ways you can keep a collection of values in your container, one way is to create and use a linked list. Arrays: You may also want to use an array to hold your elements. Using an array to hold your elements may be much easier over using a linked list. Maybe consider using an array of shared_ptr values [.] Where Do I Start? Maybe this simple skeleton will help (you will need to add quite a bit more, but it's a start). Also, begin working on this so that you will have a good framework set up by the time you come to class next week. Labs will certain y be available to help you, but I will be handing out answers! If you allow yourself to fall behind, you may have difficulty catching up. #include #include using namespace std: template class MyVector { protected: shared_ptr values []: public: MyVector(){values = new shared_ptr [100];} };

Explanation / Answer

Myvector.cpp
#include<bits/stdc++.h>
using namespace std;
template <class T>
class MyVector
{
public:

typedef T * iterator;

MyVector();
MyVector(unsigned int size);
MyVector(unsigned int size, const T & initial);
MyVector(const MyVector<T> & v);
~MyVector();

unsigned int capacity() const;
unsigned int size() const;
bool empty() const;
iterator begin();
iterator end();
T & front();
T & back();
void push_back(const T & value);
void pop_back();

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

T & operator[](unsigned int index);
MyVector<T> & operator=(const MyVector<T> &);
void clear();
private:
unsigned int my_size;
unsigned int my_capacity;
T * buffer;
};

Myvector.cpp
#include<bits/stdc++.h>
using namespace std;
template<class T>
MyVector<T>::MyVector()
{
my_capacity = 0;
my_size = 0;
buffer = 0;
}

template<class T>
MyVector<T>::MyVector(const MyVector<T> & v)
{
my_size = v.my_size;
my_capacity = v.my_capacity;
buffer = new T[my_size];
for (unsigned int i = 0; i < my_size; i++)
buffer[i] = v.buffer[i];
}

template<class T>
MyVector<T>::MyVector(unsigned int size)
{
my_capacity = size;
my_size = size;
buffer = new T[size];
}

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

template<class T>
MyVector<T> & MyVector<T>::operator = (const MyVector<T> & v)
{
delete[ ] buffer;
my_size = v.my_size;
my_capacity = v.my_capacity;
buffer = new T [my_size];
for (unsigned int i = 0; i < my_size; i++)
buffer[i] = v.buffer[i];
return *this;
}

template<class T>
typename MyVector<T>::iterator MyVector<T>::begin()
{
return buffer;
}

template<class T>
typename MyVector<T>::iterator MyVector<T>::end()
{
return buffer + size();
}

template<class T>
T& MyVector<T>::front()
{
return buffer[0];
}

template<class T>
T& MyVector<T>::back()
{
return buffer[my_size - 1];
}

template<class T>
void MyVector<T>::push_back(const T & v)
{
if (my_size >= my_capacity)
reserve(my_capacity +5);
buffer [my_size++] = v;
}

template<class T>
void MyVector<T>::pop_back()
{
my_size--;
}

template<class T>
void MyVector<T>::reserve(unsigned int capacity)
{
if(buffer == 0)
{
my_size = 0;
my_capacity = 0;
}
T * Newbuffer = new T [capacity];
//assert(Newbuffer);
unsigned int l_Size = capacity < my_size ? capacity : my_size;
//copy (buffer, buffer + l_Size, Newbuffer);

for (unsigned int i = 0; i < l_Size; i++)
Newbuffer[i] = buffer[i];

my_capacity = capacity;
delete[] buffer;
buffer = Newbuffer;
}

template<class T>
unsigned int MyVector<T>::size()
{
return my_size;
}

template<class T>
void MyVector<T>::resize(unsigned int size)
{
reserve(size);
my_size = size;
}

template<class T>
T& MyVector<T>::operator[](unsigned int index)
{
return buffer[index];
}

template<class T>
unsigned int MyVector<T>::capacity()const
{
return my_capacity;
}

template<class T>
MyVector<T>::~MyVector()
{
delete[ ] buffer;
}
template <class T>
void MyVector<T>::clear()
{
my_capacity = 0;
my_size = 0;
buffer = 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote