这就是我所拥有的,但是我不确定这是否有效。我不确定以这种方式复制是否有效,并且我无法找到过载的方式。复制时,我要提供相同的大小,将标头和尾部(第一个元素和最后一个元素的索引)设置在同一位置,并简单地将数组的每个元素复制到另一个元素中。重载在这里似乎很棘手。我之前做过重载,但从未使用过圆形数组队列。
#include <iostream>
using namespace std;
class CircArrayQueue
{
private:
int *arr;
int head;
int tail;
int size;
CircArrayQueue()
{
arr = new int[5];
head = -1;
tail = -1;
}
CircArrayQueue(int s)
{
arr = new int[s];
head = -1;
tail = -1;
size = s;
}
void copyQueue(CircArrayQueue &object)
{
object.size = this->size;
for (int i = 0; i < this->size; i++)
{
object.arr[i] = this->arr[i];
}
object.head = this->head;
object.tail = this->tail;
}
~CircArrayQueue()
{
delete[] arr;
}
};
我将要学习的完美解决方案是复制交换习惯,它可以完成工作。What is the copy-and-swap idiom?