我需要做一个深度复制,我是否正确使用了我的复制构造函数?
I need to do a deep copy, am I using my copy constructor correctly?
我需要做一个深度复制。 我是否正确使用了复制构造函数?我应该更改什么?
#include <iostream>
#include <sstream>
using namespace std;
class LinkedList
{
public:
int data;
LinkedList* prevNode;
LinkedList()
{
int dd = 0;
prevNode = nullptr;
}
LinkedList(int dd, LinkedList* pr)
{
data = dd;
prevNode = pr;
}
};
class Stack
{
private:
LinkedList* topNode;
public:
Stack();
Stack(const Stack& original);
~Stack();
bool isEmpty() const;
int top() const;
int pop();
void push(int);
};
Stack::Stack()
{
topNode = nullptr;
}
Stack::Stack(const Stack& original)
{
this->topNode = original.topNode;
}
Stack::~Stack()
{
while (!isEmpty())
{
pop();
}
}
bool Stack::isEmpty() const
{
if (topNode == NULL)
{
return true;
}
return false;
}
int Stack::top() const
{
if (isEmpty())
{
throw runtime_error("error: stack is empty");
}
return topNode->data;
}
int Stack::pop()
{
int topVal = top();
LinkedList* oldtop = topNode;
topNode = topNode->prevNode;
return topVal;
}
void Stack::push(int newData)
{
LinkedList* newNode = new LinkedList(newData, topNode);
topNode = newNode;
}
int returnNumber(string inputString)
{
istringstream fr(inputString);
int number;
while (fr >> number)
{
return number;
}
if (fr.fail())
{
throw runtime_error("error: not a number");
}
return number;
}
void list(Stack s)
{
cout << "[";
while (!s.isEmpty())
{
cout << s.pop();
if (!s.isEmpty())
{
cout << ",";
}
}
cout << "]" << endl;
}
void readCommands(Stack& newStack)
{
string command = " ";
while (command != "end")
{
cout << "stack> ";
cin >> command;
cout << endl;
if (cin.eof())
{
break;
}
try
{
if (command == "top")
{
cout << newStack.top() << endl;
}
else if (command == "pop")
{
cout << newStack.pop() << endl;
}
else if (command == "push")
{
string inputValue;
cin >> inputValue;
int number = returnNumber(inputValue);
newStack.push(number);
cin.ignore();
}
else if (command == "list")
{
list(newStack);
}
else
{
if (command != "end")
{
throw runtime_error("error: invalid command");
}
}
}
catch (runtime_error e)
{
cout << e.what() << endl;
}
}
}
int main()
{
Stack newStack;
readCommands(newStack);
return 0;
}
我需要做一个深度复制,我是否正确使用了我的复制构造函数?
不。要执行深层复制,您需要在新堆栈中分配新空间。这将看起来像这样:
class LinkedList
{
public:
static LinkedList* Copy(LinkedList* c) {
if(c == nullptr) {
return nullptr;
} else {
return new LinkedList(data, Copy(prevNode));
}
}
};
此静态函数接受任何指针并创建其值和列表的副本。
然后,在您的堆栈中:
Stack::Stack(const Stack& original)
{
this->topNode = LinkedList::Copy(original.topNode);
}
通过这样做,您实际上是在创建新的内存,而不是盲目复制指针。这就是深度复制的伟大思想。
如果你仍然对深度副本的工作原理感到困惑,我建议你咨询你的教科书或教授。
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 复制构造函数、赋值运算符C++
- std::ofstream 作为类成员删除复制构造函数?
- 复制构造函数C++无法正确复制指针
- 关于复制构造函数的一个棘手问题
- 为什么调用复制构造函数而不是移动构造函数?
- 填充上编译器生成的复制构造函数之间的不一致
- C++ 对象指针数组的复制构造函数
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 防止在复制构造函数中隐式调用基构造函数
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 具有已删除移动和复制构造函数的类的就地构造
- 复制构造函数隐式转换问题
- 复制构造函数中的递归调用