【问题标题】:How to overload operator+ in a class to add two arrays in c++?如何在类中重载 operator+ 以在 C++ 中添加两个数组?
【发布时间】:2021-04-09 05:45:51
【问题描述】:

在 c++ 中使用 + 的运算符重载来添加存储在 c++ 中的两个数组中的整数值的正确语法是什么?

在下面的代码示例中。我设法使用以下方法连接两个数组的大小:

Queue operator+(Queue &obj)
    {
        Queue total;
        total.size = this->size + obj.size;

但我不知道连接它们的值的正确语法是什么?以下不起作用。

total = this->queue + obj;
    }

完整代码如下:

#include <iostream>
using namespace std; 

class Queue { 
    int size; 
    int* queue; 
    
    public:
    Queue() { 
        size = 0;
        queue = new int[100];
    }
    void add(int data) { 
        queue[size] = data; 
        size++;
    }
    void remove() { 
        if (size == 0) { 
            cout << "Queue is empty"<<endl; 
            return; 
        } 
        else { 
            for (int i = 0; i < size - 1; i++) { 
                queue[i] = queue[i + 1]; 
            } 
            size--; 
        } 
    } 
    void print() { 
        if (size == 0) { 
            cout << "Queue is empty"<<endl; 
            return; 
        } 
        for (int i = 0; i < size; i++) { 
            cout<<queue[i]<<" <- ";
        } 
        cout << endl;
    }
    
    Queue operator+(Queue &obj)
    {
        Queue total;
        total.size = this->size + obj.size;
       
        total = this->queue + obj;
        
        return total;
            
    }
} ;

int main() { 
    Queue q1; 
    q1.add(42); q1.add(2); q1.add(8);  q1.add(1);
    Queue q2;
    q2.add(3); q2.add(66); q2.add(128);  q2.add(5);
    Queue q3 = q1+q2;
    q1.print();
    q2.print();
    q3.print();
    
    

    return 0; 
} 

【问题讨论】:

  • 没有正确的语法。您必须分配一个具有两个较小数组大小的新数组,然后将两个较小数组中的内容复制到新的大数组中。这是喜欢标准库容器的众多原因之一,例如 std::vector 而不是 c 样式的数组。

标签: c++ arrays operator-overloading concatenation


【解决方案1】:

这是一个很长的答案,希望能更好地理解正在发生的事情。

除了原始构造函数之外,您还需要一个构造函数来进行重载。

Queue(int a, int* b)
        : size(a), queue(b) { }

这将允许您通过指针访问数组。原始构造函数初始化为 100 个元素。因此,我使用循环添加第一个数组,然后继续添加第二个数组,而不是将元素 q1 和 q2 的值相加。请看下面:

#include <iostream>
using namespace std;

class Queue {
    int size;
    int* queue;

public:

    Queue() {
        size = 0;
        queue = new int[100];
    }
    Queue(int a, int* b)
        : size(a), queue(b) { }

    void add(int data) {
        queue[size] = data;
        size++;
    }
    void remove() {
        if (size == 0) {
            cout << "Queue is empty" << endl;
            return;
        }
        else {
            for (int i = 0; i < size - 1; i++) {
                queue[i] = queue[i + 1];
            }
            size--;
        }
    }
    void print() {
        if (size == 0) {
            cout << "Queue is empty" << endl;
            return;
        }
        for (int i = 0; i < size; i++) {
            cout << queue[i] << " <- ";
        }
        cout << endl;
    }
    Queue operator+(Queue &obj) {
        Queue ans;
        int total_size;
        int x;

        total_size = this->size + obj.size;
    

        for (x = 0; x < this->size; x++) {
            ans.queue[x] = this->queue[x];
        
        }
        for (x = this->size; x < total_size; x++) {
            ans.queue[x] = obj.queue[x - this->size];
        
        }
        ans.size = this->size+obj.size;
        return ans;

    }


};

int main() {
    Queue q1;
    q1.add(42); q1.add(2); q1.add(8);  q1.add(1);
    Queue q2;
    q2.add(3); q2.add(66); q2.add(128);  q2.add(5);
    Queue q3 = q1 + q2;
    q3.print();

    return 0;
}

这解决了问题,但我确信有一个更优雅的连接数组的解决方案。

【讨论】:

    【解决方案2】:

    可以通过从对象中复制数组来进行连接。

    #include <algorithm> // for std::copy()
    
        Queue operator+(Queue &obj)
        {
            Queue total;
            total.size = this->size + obj.size;
           
            if (total.size > 100) {
                cout << "Too long"<<endl;
            } else {
                std::copy(this->queue, this->queue + this->size, total->queue);
                std::copy(obj.queue, obj.queue + obj.size, total->queue + this->size);
            }
    
            
            return total;
                
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2010-12-14
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      相关资源
      最近更新 更多