-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hw02 && 模板 && 迭代器 #53
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 避免函数参数不必要的拷贝 4/5 分
- 修复智能指针造成的问题 10/10 分
- 改用
unique_ptr<Node>
7/10 分 - 实现拷贝构造函数为深拷贝 13/15 分
- 说明为什么可以删除拷贝赋值函数 4/5 分
- 改进
Node
的构造函数 5/5 分 - 能够在 PR 描述中用自己的话解释 25/25 分
- 代码格式规范、能够跨平台 5/5 分
- 有自己独特的创新点 18/20 分
91分!
改进建议:实现 const_iterator 和 cbegin/cend。
}; | ||
|
||
void print(List lst) { // 有什么值得改进的? | ||
void print(List<int> &lst) { // 有什么值得改进的?: 传入const引用,避免拷贝 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void print(List<int> &lst) { // 有什么值得改进的?: 传入const引用,避免拷贝 | |
void print(List<int> const &lst) { // 有什么值得改进的?: 传入const引用,避免拷贝 |
|
||
List() = default; | ||
|
||
List(List const &other) { | ||
List(List &other) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List(List &other) { | |
List(List const &other_) { | |
List &other = const_cast<List &>(other_); |
拷贝构造函数有特定的函数签名,不建议轻易改变。
auto curr = front(); | ||
for (size_t i = 0; i < index; i++) { | ||
curr = curr->next.get(); | ||
} | ||
return curr; | ||
} | ||
|
||
iterator begin() { return iterator(head.get()); } | ||
iterator end() { return iterator(nullptr); } | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以在这里声明两个 const 版本的 begin 和 end。
作业要求
print()
函数以及push_front()
函数中可以将参数类型改为常量引用,避免拷贝unique_ptr
+Node*
的方式解决unique_ptr<Node>
10 分head
的unique_ptr
push_back(value_type const &)
函数即可Node
的构造函数 5 分改为模板
Node
,List
均定义为模板value
为模板类型List
中成员函数的某些参数或返回值类型为对应的模板类型迭代器
参考: Prepare the custom iterator
第一次自定义迭代器,使用了
std::bidirectional_iterator_tag
,没有定义const_iterator。