Skip to content
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

done the hw3 #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 62 additions & 31 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,86 @@
#include <algorithm>
#include <iostream>
#include <vector>
#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;
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>
std::vector<T0> operator+(std::vector<T1> const &a, std::vector<T2> const &b) {
// 请实现列表的逐元素加法!10 分
// 例如 {1, 2} + {3, 4} = {4, 6}
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>
std::variant<T1, T2> operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) {
// 请实现自动匹配容器中具体类型的加法!10 分
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) {
// 请实现自动匹配容器中具体类型的打印!10 分
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;
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;
// 应该输出 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;
// 应该输出 {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;
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;
// 应该输出 {9.28, 17.436, 7.236}
std::cout << d << std::endl;

return 0;
return 0;
}