Question: Using C++, implement an efficient template operator - function that takes two pre-sorted Vectors called v1 and v2 which are provided as parameters to the

Using C++, implement an efficient template operator- function that takes two pre-sorted Vectors called v1 and v2 which are provided as parameters to the operator. The operator function returns a Vector that has all the elements of v2 that are common in v1 removed from v1. The following usage is anticipated where v3 is another Vector. The Vector class to be used is from your own. You have shown this at an earlier question. STL data structures or algorithms or other library routines cannot be used in this question.

v3 = v1 - v2;

As an example, if Vector v1 contains (2, 8, 12 17, 22), v2 contains (17, 18, 22, 30), after the above line of code, v3 will contain (2, 8, 12). Vectors v1 and v2 contain sorted data and must not be modified. Vector v3 remains sorted. Make sure your code works when tested in Codeblocks.

I have done some parts, something like

template

void operator(vector v1, vector v2, vector &v3);

template

void operator(vector v1, vector v2, vector &v3)

{

int i=0, j=0;

T num1=v1.at(0);

T num2=v2.at(0);

while(i

{

num1 = v1.at(i);

num2= v2.at(j);

if(num1

{

v3.push_back(num1);

i++;

}

else if(num2 < num1)

{

j++;

}

else

{

i++;

j++;

}

while(i

{

v3.push_back(v1.at(i++));

}

}

}

But not working....Could you please help me to figure out what to do?

We use C++ ISO (std=++11) compiler only and below is my Vector class

#ifndef Vector_h #define Vector_h #include #include #include #include #include

using namespace std;

template

class Vector { public: Vector(); ~Vector(); void push_back(const T& i); T& at(int i); void resize(int newSize); int size();

private: int length; int arr; T *list; };

template Vector::Vector() { length=0; arr=100; list=new T[arr]; } template Vector::~Vector() { delete[]list; } template void Vector::resize(int newSize) { T *newList=new T[arr]; T *newList1=newList; T *oldList1=list; while(oldList1 != (list+length)) { *(newList1)=*(oldList1); newList1++; oldList1++; } arr=newSize; delete[] list; list=newList; }

template void Vector::push_back(const T& i) { if((arr/2)==length) { Vector::resize(arr*1.5); } list[length]=i; length++; } template T& Vector ::at(int i) { if((i>=0)&&(i int Vector::size() { return length; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!