-
Notifications
You must be signed in to change notification settings - Fork 74
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
hw03 Gelee #18
base: main
Are you sure you want to change the base?
hw03 Gelee #18
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.
- 完成作业基本要求 47/50 分
- 能够在 PR 描述中用自己的话解释 22/25 分
- 代码格式规范、能够跨平台 2/5 分
- 有自己独特的创新点 15/20 分
|
||
template <class T1, class T2> | ||
auto operator+(std::variant<T1, T2> const &a, T2 const &b) { | ||
std::variant<T1, T2> b1 = b; |
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.
独特的创新点!
// } | ||
//}; | ||
|
||
|
||
|
||
template <class T1, class T2> | ||
std::ostream &operator<<(std::ostream &os, std::variant<T1, T2> const &a) { | ||
// 请实现自动匹配容器中具体类型的打印!10 分 |
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.
8
|
||
template <class T1, class T2> | ||
std::ostream &operator<<(std::ostream &os, std::variant<T1, T2> const &a) { | ||
// 请实现自动匹配容器中具体类型的打印!10 分 | ||
std::visit([&](auto const & it){ | ||
os << it << std::endl; |
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.
这里不需要 endl 哦!一般调用该函数时候 cout << d << endl
已经有一个了。
} | ||
|
||
template <class T1, class T2> | ||
std::variant<T1, T2> operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) { | ||
// 请实现自动匹配容器中具体类型的加法!10 分 | ||
} | ||
return std::visit([&] (auto const &t1, auto const &t2) | ||
->std::variant<T1 ,T2>{ |
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.
做的好!直接在返回类型上构造,避免了额外的默认构造+拷贝赋值。
for(int i =0; i < n ; i++){ | ||
res[i] = a[i]+b[i]; | ||
} | ||
return res; | ||
} | ||
|
||
template <class T1, class T2> | ||
std::variant<T1, T2> operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) { | ||
// 请实现自动匹配容器中具体类型的加法!10 分 |
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.
10
// 请实现列表的逐元素加法!10 分 | ||
// 例如 {1, 2} + {3, 4} = {4, 6} | ||
using T0 = decltype(T1{} + T2{}); | ||
const int n = std::min(a.size(),b.size()); | ||
std::vector<T0> res(n,0); |
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.
不需要指定右边的 0,反而是如果 T0 不是一个数值类型这样会失败,直接 std::vector(n) 就可以调用数值类型 T0 的默认的构造函数 - 零初始化了。
@@ -16,19 +17,68 @@ std::ostream &operator<<(std::ostream &os, std::vector<T> const &a) { | |||
|
|||
// 请修复这个函数的定义:10 分 |
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.
9
@@ -3,6 +3,7 @@ | |||
#include <variant> | |||
|
|||
// 请修复这个函数的定义:10 分 |
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.
10
修复第一个函数:简单的加上template即可;
修复第二个函数:由下面的实际运算结果可知,结果的vector与二者中最短的相同,逐项相加即可。
自动匹配容器中具体类型的方法:
此“+” 为 std::variant<T1, T2> + std::variant<T1, T2>; 使用课上讲的std::visit,有多个variant作为参数,visit会自动地罗列出所
有的排列组合。
自动匹配容器中具体类型的打印: 同理的使用std::visit即可。
关于
d + c + e
:编译器给的报错是:
由此有了两种思路:
对于
d + c
,重载运算符+
,可以将c变为d的类型后,调用上面已写好的std::variant<T1, T2> + std::variant<T1, T2>
的重载运算符+
即可,这样子其实d + c + e 已经可以运算。但是若有表达式d = c + e
, 则是不可以的。为了防止未知的情况,所以对于c
在运算符的左边的情况也需要重载。第二种思路则是利用
std::hold_alternative
或者v.index
来获取std::variant<T1, T2>
类型,然后对于d + c, c + e
重载运算符将+
时,使用std::get<index>()
将d ,e
变为确定的类型, 然后调用关于std::vector<T1> + std::vector<T2>
的重载运算符“+”即可。