Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Answer
Q1
单纯从函数的定义上来说,缺少template T的定义,因此修复非常简单:
Q2
这个题目其实做起来挺纠结的,因为在template的定义上来说,缺少了T0的定义,如果template的定义无需修复的话,那么我们首先修复的是function的return data type。使用auto来代替std::vector
如果template的定义需要修复的话,首先应该定义T0, 并且预设T0的data type. 在测试代码中,因为输入数据只含有int, double两种类型,在这里 T0=double
Q3
这个题目有两种解决思路:
但是,在测试函数中出现了std::vector<T0>和std::variant<std::vector<T1>, std::vector<T2>>进行相加的情况。我在这里针对这种情况overload了“+”
Q4
函数的具体思路跟之前的Q3中一样,利用std::visit来实现相应的功能。
Q5【关于内卷】
题目:把 variant 的 operator<< 改成了基于变长模板参数的
实现思路,其实只是variant中的data type的长度变得不确定了。处理方式参照我们之前的Q4的处理方式就行。但是需要有一些修改。
实现代码: