0

我需要实现运算符 + & - 和重复删除向量'元素。我怎样才能使操作员功能,使输出变成这样:

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 ] 

主要部分:

#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;
  }

这是没有运算符功能的代码:

#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();
    }

卡在这里:

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

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

void duplicate(size_t index, size_t n){
}

void removeDuplicate(){}    



};
4

1 回答 1

1

对于 + 运算符,检查std::vector::insert

对于 - 运算符,此链接

要创建副本,您必须简单地使用std::sort对向量进行排序,但从您的示例中不清楚。

要删除重复项,此链接可能会有所帮助。

您必须简单地将这些信息应用于您的运算符和方法。

于 2013-10-16T13:51:06.480 回答