Question

I need to implement the operator + & -, and duplicate-removal vector' element. How can i made the operator function so that the output become like this :

b1 : [ 1 2 3 4 ]
b2 : [ 2 4 6 8 10 12 14 16 18 20 ]
b1 + b2 : [ 1 2 3 4 2 4 6 8 10 12 14 16 18 20 ] 
b1 - b2 : [ 1 3 ]
b2 duplicate : [ 1 2 2 4 4 6 6 8 8 10 12 14 16 18 20 ]
b2 remove duplicate : [ 1 2 4 6 8 10 12 14 16 18 20 ] 

the main part :

#include <iostream>
#include <cstdlib>
#include "MyNumber.cpp"
using namespace std;

int main() {
MyNumber<int> b1;
MyNumber<int> b2;

    for(int i=1; i<=3;i++)
    {
            b1.push_back(i);
    }
for(int i=1;i<=10;i++)
{
    b2.push_back(i*2);
}

cout << "b1 : " << b1.toString() << endl;
cout << "b2 : " << b2.toString() << endl;

cout << "b1 + b2 : " << (b1 + b2).toString() << endl << endl;
cout << "b1 - b2 : " << (b1 - b2).toString() << endl;

for(int i=0;i<4;i++){
    b2.duplicate(i+i,1);
}

b2.insert(0,1);
cout << "b2 duplicate : " << b2.toString() << endl;

b2.removeDuplicate();
cout << "b2 remove duplicate : " << b2.toString() << endl << endl;



return 0;
  }

Here is the code without the operator function :

#include <iostream>
#include <string>
#include <sstream>
#include <stdexcept>
#include <algorithm>

using namespace std;

template <typename B>
class MyNumber
{
private :
    static const size_t BEGINNING_CAPACITY =10;
    size_t _capacity;           
    size_t _size;       
    B* _data; // array' element

public :
    // Constructor
    MyNumber<B>() : _capacity(BEGINNING_CAPACITY),
                _size(0),
                    _data(new B[BEGINNING_CAPACITY])
    {}

    //Destructor
    ~MyNumber<B>()
    {
        delete[] _data;
    }

    //Copy Constructor
    MyNumber<B>(const MyNumber<B>& OtherNumber) :
                    _capacity(OtherNumber._capacity),
                    _size(OtherNumber._size),
                    _data(new B[_capacity])
    {
        for(size_t i = 0; i < _size; i++)
            _data[i] = OtherNumber._data[i];
    }

    // template function swap STL algorithm
    void swap(MyNumber<B>& OtherNumber)
    {
        swap(_size, OtherNumber._size);
        swap(_capacity, OtherNumber._capacity);
        swap(_data, OtherNumber._data);
    }

    MyNumber<B>& operator= (const MyNumber<B>& OtherNumber)
    {

        MyNumber<B> copy(OtherNumber);
        exchange(copy);
        return *this;
    }

    // Operator indexing []
    B& operator[] (size_t index)
    {
        if(index < 0 || index >= _size)
        {
            throw out_of_range("Index operator [] out of range");
        }
        return _data[index];
    }

    //Function for adding new element
    void push_back(const B& elemen)
    {
        if(_size == _capacity)
        {
            expand(2 *_capacity);
        }
        _data[_size] = elemen;
        _size++;
    }

    //Function for inserting
    void insert(size_t index, const B& elemen)
    {
        if(index < 0 || index > _size) 
        {
            throw out_of_range("index insert out of range");
        }
        if(_size == _capacity)
        {
            expand(2 * _capacity);
        }

        for(size_t i = _size; i > index; i--)
        {
            _data[i] = _data[i-1];
        }
        _data[index] = elemen;
        _size++;
    }


    //Function for representing the vector
    string toString()
    {
        ostringstream oss;
        oss << "[ ";
        for(int i = 0; i < _size; ++i)
            oss << _data[i] << " ";
        oss << "]";
        return oss.str();
    }

Stuck here :

MyNumber<B>& operator+ (MyNumber<B>& OtherNumber){
}

MyNumber<B>& operator- (MyNumber<B>& OtherNumber){
}   

void duplicate(size_t index, size_t n){
}

void removeDuplicate(){}    



};
Was it helpful?

Solution

For + operator, check std::vector::insert.

For - operator, this link.

To create the duplicate, you must simply sort the vector with std::sort but it's not clear from your example.

To remove duplicates, this link can be helpful.

You must simply apply these informations on your operators and methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top