gpt4 book ai didi

c++ - 赋值运算符复制数组中除可用空间以外的所有内容

转载 作者:行者123 更新时间:2023-11-30 05:26:38 25 4
gpt4 key购买 nike

我最近一直在研究动态分配和队列。请记住,我对数据结构还很陌生,我一直在通过观看 YouTube 教程和阅读网站上的一些教程来自学,所以如果我不知道关于这个主题的每一件事,请原谅我的无知。但是,在尝试为我的队列实现赋值运算符时,我似乎在其代码中的某个地方犯了一个错误。每当我尝试使用赋值运算符时,你都会看到

Queue names2;
names2 = names;

赋值运算符复制队列中的所有内容,除了数组的空元素。我是否错误地实现了我的复制构造函数或我的赋值运算符,导致它没有将空的未使用容量添加到新创建的队列中?

main.cpp

  #include "Queue.h";

int main()
{
Queue names;
names.enqueue("A");
names.enqueue("B");
names.enqueue("C");
names.dequeue();
names.enqueue("D");
names.showFullQueue();
Queue names2;
names2 = names;
names2.showFullQueue();
names2.enqueue("Tom");
names2.enqueue("Alice");
names2.showFullQueue();

return 0;
}

Queue.h

#ifndef Queue_H
#define Queue_H

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>

using namespace std;

class Queue {
public:
Queue(); //Defualt costructor
Queue(const Queue& source); //Copy constructor
~Queue(); //Destructor

Queue& operator=(const Queue& source); //Assignament Operator //NOT WORKING

void enqueue(string value); // add item to queue
string dequeue(); // remove item from queue

void showFullQueue() const;

//memory allocation
void memory(int currentCapacity);
private:
void shift();
string * arr;
const static int front = 0;
int back;
int capacity;
int usedCapacity;
};


#endif // !Queue_H

队列.cpp

#include "Queue.h"

Queue::Queue()
{
back = -1;
capacity = 1;
usedCapacity = 0;
arr = new string[capacity];
}

Queue::Queue(const Queue & source)
{
cout << "Invoking copy constructor" << endl;
this->back = source.back;
this->capacity = source.capacity;
this->usedCapacity = source.usedCapacity;
arr = new string[source.capacity];
copy(source.arr, source.arr + usedCapacity, this->arr);
}

Queue::~Queue()
{
delete[] arr;
}

Queue & Queue::operator=(const Queue & source)
{
if (this == &source)
return *this;
cout << "Invoking Assignament operator" << endl;
Queue temp(source);
swap(temp.back, back);
swap(temp.capacity, capacity);
swap(temp.usedCapacity, capacity);
swap(temp.arr, arr);

return *this;
}

void Queue::enqueue(string value)
{
++back;
arr[back] = value;
usedCapacity++;
memory(usedCapacity);
}

string Queue::dequeue()
{
string returnValue = arr[front];
shift();
usedCapacity--;
memory(usedCapacity);
return returnValue;
}

void Queue::showFullQueue() const
{
cout << "Front: " << front << endl;
cout << "Back: " << back << endl;
for (int i = 0; i < capacity; i++)
{
cout << arr[i] << ", ";
}
cout << endl;
}

void Queue::memory(int currentCapacity)
{
if (currentCapacity >= capacity)
{
int newCapacity = (currentCapacity * 3) / 2 + 1;
string * arr2 = new string[newCapacity];
copy(arr, arr + usedCapacity, arr2);
delete[] arr;
arr = arr2;
capacity = newCapacity;
}
else
cout << "allocation error";
}

void Queue::shift()
{
for (int i = front; i <= back; i++)
{
arr[i] = arr[i + 1];
}
--back;
}

如有任何帮助,我们将不胜感激。先谢谢你。

最佳答案

好吧,这里有几个错误需要修复..

  • 您对数据成员 back 和静态成员 front 的使用对我来说似乎是多余的。

  • 我没有看到 shift 函数有任何有益的用途。

此外,在您的复制赋值运算符中...

Queue & Queue::operator=(const Queue & source)
{
if (this == &source)
return *this;
cout << "Invoking Assignament operator" << endl;
Queue temp(source);
swap(temp.back, back);
swap(temp.capacity, capacity);
swap(temp.usedCapacity, capacity); //Notice the Bug here...?
swap(temp.arr, arr);

return *this;
}

好吧,这是一个清理过的版本...(注意一些变化,比如在头文件的全局范围内using namespace std;;并且移动不必要的#includes 到实现文件...

Queue.h

#ifndef Queue_H
#define Queue_H

#include <string>

//using namespace std; ...typically a bad idea in header files

class Queue {
public:
Queue(); //Defualt costructor
Queue(const Queue& source); //Copy constructor
~Queue(); //Destructor

Queue& operator=(const Queue& source); //Assignment Operator // WORKING :-)

void enqueue(std::string value); // add item to queue
string dequeue(); // remove item from queue

void showFullQueue() const;

private:
std::string* arr;
int capacity;
int usedCapacity;

//memory allocation
void memory(int currentCapacity);
};


#endif // !Queue_H

队列.cpp

#include "Queue.h"
//You can put the remaining includes here
#include <iostream>
#include <algorithm>

using namespace std; // You can us it in your cpp file... Not a bad idea.. :-)

Queue::Queue()
{
capacity = 3;
usedCapacity = 0;
arr = new string[capacity]();
}

Queue::Queue(const Queue & source)
{
cout << "Invoking copy constructor" << endl;
capacity = source.capacity;
usedCapacity = source.usedCapacity;
arr = new string[source.capacity]();
copy(source.arr, source.arr + source.capacity, arr);
}

Queue::~Queue()
{
delete[] arr;
}

Queue & Queue::operator=(const Queue & source)
{
if (this == &source)
return *this;
cout << "Invoking Assignament operator" << endl;

using std::swap; //For ADL: ...redundant here... since you already have using namespace std;
Queue temp(source);
swap(temp.capacity, capacity);
swap(temp.usedCapacity, usedCapacity);
swap(temp.arr, arr);

return *this;
}

void Queue::enqueue(string value)
{
memory(usedCapacity);
arr[usedCapacity] = value;
++usedCapacity;
}

string Queue::dequeue()
{
string returnValue = arr[--usedCapacity];
//memory(usedCapacity); //needless
return returnValue;
}

void Queue::showFullQueue() const
{

//This styple prevents printing the last comma on the last item
int i = 0;
for (; i < usedCapacity-1; i++)
{
cout << arr[i] << ", ";
}
cout << arr[i] << endl;
}

void Queue::memory(int currentCapacity)
{
if (currentCapacity >= capacity)
{
int newCapacity = (currentCapacity * 3) / 2 + 1;
string* arr2 = new string[newCapacity]();
copy(arr, arr + capacity, arr2);
delete[] arr;
arr = arr2;
capacity = newCapacity;
}
}

并且您的 ma​​in.cpp 没有任何变化 ...:-)看到它工作 here

当然,我们还可以做一些改进

关于c++ - 赋值运算符复制数组中除可用空间以外的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37664881/

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