Skip to content

hw02 模板、迭代器 #74

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

hw02 模板、迭代器 #74

wants to merge 2 commits into from

Conversation

WAIXv
Copy link

@WAIXv WAIXv commented Oct 5, 2023

作业要求

  • 避免函数参数不必要的拷贝 5 分

    • print()push_front()改为常量参数传递,避免拷贝
  • 修复智能指针造成的问题 10 分

    • shared_ptr循环引用造成内存无法释放
  • 改用 unique_ptr<Node> 10 分

  • 实现拷贝构造函数为深拷贝 15 分

    • 使用迭代器遍历目标,开辟新内存并赋值
  • 说明为什么可以删除拷贝赋值函数 5 分

    • 编译器会自动创建一个纯右值,再用移动构造将所有权转给等号左边的变量,相当于调用了右值构造+移动赋值
  • 改进 Node 的构造函数 5 分

    • 声明为explicit,防止单参数隐式构造,初始列构造减少一次赋值

模板

List和Node类都实现为模板类,并用List<double>测试成功

迭代器

实现了list_iterator,拷贝构造时深拷贝的遍历操作使用迭代器完成

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant