gpt4 book ai didi

c++ - 当数组大小已满时如何将循环数组的内容复制到更大的数组中

转载 作者:行者123 更新时间:2023-11-28 05:10:40 24 4
gpt4 key购买 nike

我已经使用循环数组实现了队列 ADT。但是,我希望能够将圆形数组的所有内容复制到一个更大的数组中。我读过 system.arraycopy in Java,但这个函数在 C++ 中不存在。在下面的入队函数中,如果数组已满,它应该复制到一个更大的数组中,而不是抛出错误。

我的代码:

template <class Obj>

class Circular_Queue
{
int MAX;
private:
Obj *cqueue_arr;
Obj *cqueue_arr2;
int front, rear;
public:
Circular_Queue()
{
cqueue_arr = NULL;
cout << "Enter size of the queue:";
cin >> MAX;
cqueue_arr = new Obj[MAX];
rear = -1, front = -1;
}

~Circular_Queue() {
delete[]cqueue_arr;
}

void qfront() {
if (front == -1)
{
throw QueueEmptyException();
}
cout << "Front item is : " << cqueue_arr[front] << endl;
}

//Insert into Circular Queue
void enqueue(Obj item)
{
int i;

if ((front == 0 && rear == MAX - 1) || (front == (rear + 1)% MAX))
{
Obj* cqueue_arr2 = new Obj[MAX * 2]; // Creates a bigger array.
for (int i = 0; i < (MAX * 2); i++) { // This section doesnt workas intended.
cqueue_arr2[i] = cqueue_arr[i];
}

}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1)
rear = 0;
else
rear = ((rear + 1) % MAX);
}
cqueue_arr[rear] = item;
cout << "Insertion Success!!!\n";
cout << "The number of space left in the queue is ";
cout << MAX - (rear + 1) << endl;
cout << " \n";
cout << "Content of new array is:";
for (int i = 0; i < MAX * 2; i++)
cout << cqueue_arr[i] << " ";
}

// Delete from Circular Queue
Obj dequeue()
{
if (front == -1)
{
throw QueueEmptyException();
}
cout << "Element deleted from queue is : " << cqueue_arr[front] << endl;
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1)
front = 0;
else
front = ((front + 1) % MAX);
}
}

//Display Circular Queue

void display()
{
int front_pos = front, rear_pos = rear;
if (front == -1)
{
cout << "Cannot display, Queue is EMPTY!\n";
return;
}
cout << "Circular Queue elements are:\n";
if (front_pos <= rear_pos)
{
while (front_pos <= rear_pos)
{
cout << cqueue_arr[front_pos] << " ";
front_pos++;
}
}
else
{
while (front_pos <= MAX - 1)
{
cout << cqueue_arr[front_pos] << " ";
front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
cout << cqueue_arr[front_pos] << " ";
front_pos++;
}
}
cout << endl;
}
};

最佳答案

主要问题是您正在越界访问数组。您应该遍历现有尺寸,而不是新尺寸。此外,您只是丢弃了新数组,您需要删除旧数组并交换新数组,即

if ((front == 0 && rear == MAX - 1) || (front == (rear + 1)% MAX))
{
auto newMax = MAX * 2;
Obj* cqueue_arr2 = new Obj[newMax]; // Creates a bigger array.
for (int i = 0; i < (MAX); i++) { // loop over original array size, not new size
cqueue_arr2[i] = cqueue_arr[i];
}
// remember to actually use the new array, and discard the old one.
delete[] cqueue_arr;
cqueue_arr = cqueue_arr2
MAX = newMax;
}

顺便说一句,仅使用 std::vector 而不是 Data[] 将使所有这些都变得毫无意义。它会有效地 self 增长,尽可能避免复制(类似于 realloc())。它还将避免一些其他限制(例如,您当前的实现需要默认构造函数和复制赋值运算符),并且所有访问都将内联到指针算法,因此您不会通过使用原始指针获得任何好处。实现在很大程度上可以保持不变,除了增长的部分,它只是变成 queue_vec.push_back(val);

此外,在构造函数中使用 cin 是不好的做法。您应该只传递一个 int 作为参数。如果需要,您可以从 main() 中的 cin 读取它。这样你的类就可以在别处使用而不会干扰 IO。

关于c++ - 当数组大小已满时如何将循环数组的内容复制到更大的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43577295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com