Question

Implementing the vector class in the following code (It's C++, not Java). You just need to...

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 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 <iostream>

#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

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

implementing vector:-

Add a comment
Know the answer?
Add Answer to:
Implementing the vector class in the following code (It's C++, not Java). You just need to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

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

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

  • C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class...

    C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program. Submit three files: YourLastNameFirstNameInitialMoney.cpp - This file contains only the Money class implementation YourLastNameFirstNameInitialProj4.cpp - This file contains only the main function to use/test your money class YourLastNameFirstNameInitialMoney.h - This file contains the header file information.   Optional bonus (10%): Do some research on make...

  • Take the following code for a binary source tree and make it include the following operations....

    Take the following code for a binary source tree and make it include the following operations. bool replace(const Comparable & item, const Comparable & replacementItem); int getNumberOfNodes() const; (a) The replace method searches for the node that contains item in a binary search tree, if it is found, it replaces item with replacementItem. The binary tree should remain as a binary search tree after the replacement is done. Add the replace operation to the BinarySearchTree class. Test your replace using...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT