可视化 如何实现 c++ 列表的运算符=?
visual How to implement c++ list's operator=?
这是一个关于实现c++列表的任务,所以如果有人问"为什么不使用[非常方便的东西]而不是列表",答案是问我的教授,而不是我。我也不能更改我的标题。请注意,此版本的列表只需要能够接受int数据类型。假设我已经实现了列表的所有其他功能。
首先,我尝试过这个方法,但没有成功,因为我意识到我不能迭代l,它是常数(所以没有l.curr=l.curr->next等(。
Linkedlist& Linkedlist::operator=(const Linkedlist& l)
{
if (this != &l)
{
this->clear();
l.curr = l.head;
for (int i = 0; i < l.numElem; i++)
{
this->push_back(l.curr->data);
}
}
return *this;
}
我也试过这个,但再一次我不能以任何方式修改l的形状或形式,因为它是恒定的。类型限定符不兼容。
Linkedlist& Linkedlist::operator=(const Linkedlist& l)
{
if (this != &l)
{
this->clear();
for (int i = 0; i < l.numElem; i++)
{
this->push_back(l.front());
l.pop_front();
}
}
return *this;
}
抱歉/如果我需要提供更多信息,请告诉我。
我无法迭代
l
,它是常量(因此没有l.curr = l.curr->next
等(。
这不是迭代对象节点的方式。您可以使用:
for (auto iter = l.head; iter != nullptr; iter = iter->next )
{
}
在您的情况下,您可以使用:
Linkedlist& Linkedlist::operator=(const Linkedlist& l)
{
if (this != &l)
{
this->clear();
for (auto iter = l.head; iter != nullptr; iter = iter->next )
{
this->push_back(iter->data);
}
}
return *this;
}
相关文章:
- 链接列表运算符重载没有打印出我想要的内容
- 将异类初始值设定项列表传递给流运算符
- 如何在可观察量列表中使用 RxCpp 运算符?
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 可视化 如何实现 c++ 列表的运算符=?
- 使用继承的指针列表复制构造函数或重载运算符=
- 提升精神气中的序列和列表运算符的属性?
- 错误:运算符 [] 不匹配。在比较列表中的 int 和 int 时<int>,
- 使用初始值设定项列表作为函数参数的类对象实现运算符 [] 的示例
- 如何重载运算符<<显示我的列表
- C++ 链表列表运算符[]
- 我可以在 Boost.Spirit.Qi 中向列表运算符 (%) 提供内存分配提示吗?
- 为什么我可以在不使用赋值运算符的情况下使用列表初始化普通数组
- 如何让迭代器使用赋值运算符对列表进行操作
- 复制构造函数和运算符"="他们没有打印列表 l 的副本
- 为什么C++不允许在条件运算符中进行隐式列表初始化?
- 多项式链接列表中的过载 运算符
- 将[]运算符与链接列表一起使用
- 与链接列表运算符中的传递值(例如C 标头文件中的删除(front))中,我如何使用布尔函数
- 速推中的关键字列表运算符