forked from parallel101/hw03
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
86 lines (74 loc) · 2.54 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <algorithm>
#include <iostream>
#include <type_traits>
#include <variant>
#include <vector>
// 请修复这个函数的定义:10 分
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> const &a) {
os << "{";
for (size_t i = 0; i < a.size(); i++) {
os << a[i];
if (i != a.size() - 1)
os << ", ";
}
os << "}";
return os;
}
// 请修复这个函数的定义:10 分
template <class T1, class T2>
auto operator+(std::vector<T1> const &a, std::vector<T2> const &b) {
using T0 = decltype(T1{} + T2{});
auto lens = std::min(a.size(), b.size());
std::vector<T0> res(lens);
if (lens == a.size()) {
std::transform(a.begin(), a.end(), b.begin(), res.begin(), std::plus<>());
} else {
std::transform(b.begin(), b.end(), a.begin(), res.begin(), std::plus<>());
}
return res;
// 请实现列表的逐元素加法!10 分
// 例如 {1, 2} + {3, 4} = {4, 6}
}
template <class T1, class T2, class U>
std::variant<T1, T2> operator+(std::variant<T1, T2> const &a, const U &b) {
using tb = std::decay_t<U>;
std::variant<T1, T2> res;
if constexpr (std::is_same_v<T1, tb> || std::is_same_v<T2, tb>) {
std::visit([&](const auto &x) { res = x + b; }, a);
} else if constexpr (std::is_same_v<std::variant<T1, T2>, tb>) {
std::visit([&](const auto &x, const auto &y) { res = x + y; }, a, b);
}
return res;
}
// template <class T1, class T2>
// std::variant<T1, T2> operator+(std::variant<T1, T2> const &a,
// std::variant<T1, T2> const &b) {
// std::variant<T1, T2, decltype(T1{} + T2{})> res{};
// std::visit([&](const auto &a, const auto &v) { res = a + b; }, a, b);
// return res;
// // 请实现自动匹配容器中具体类型的加法!10 分
// }
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::variant<T1, T2> const &a) {
std::visit([&](const auto &a) { os << a; }, a);
return os;
// 请实现自动匹配容器中具体类型的打印!10 分
}
int main() {
std::vector<int> a = {1, 4, 2, 8, 5, 7};
std::cout << a << std::endl;
std::vector<double> b = {3.14, 2.718, 0.618};
std::cout << b << std::endl;
auto c = a + b;
// 应该输出 1
std::cout << std::is_same_v<decltype(c), std::vector<double>> << std::endl;
// 应该输出 {4.14, 6.718, 2.618}
std::cout << c << std::endl;
std::variant<std::vector<int>, std::vector<double>> d = c;
std::variant<std::vector<int>, std::vector<double>> e = a;
d = d + c + e;
// 应该输出 {9.28, 17.436, 7.236}
std::cout << d << std::endl;
return 0;
}