forked from parallel101/hw03
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <cstdlib> | ||
#if defined(__GNUC__) || defined(__clang__) | ||
#include <cxxabi.h> | ||
#endif | ||
|
||
template <class T> | ||
std::string cpp_type_name() { | ||
const char *name = typeid(T).name(); | ||
#if defined(__GNUC__) || defined(__clang__) | ||
int status; | ||
char *p = abi::__cxa_demangle(name, 0, 0, &status); | ||
std::string s = p; | ||
std::free(p); | ||
#else | ||
std::string s = name; | ||
#endif | ||
if (std::is_const_v<std::remove_reference_t<T>>) | ||
s += " const"; | ||
if (std::is_volatile_v<std::remove_reference_t<T>>) | ||
s += " volatile"; | ||
if (std::is_lvalue_reference_v<T>) | ||
s += " &"; | ||
if (std::is_rvalue_reference_v<T>) | ||
s += " &&"; | ||
return s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,55 @@ | ||
#include <cstdio> | ||
#include <iostream> | ||
#include <vector> | ||
#include <variant> | ||
|
||
// 请修复这个函数的定义:10 分 | ||
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> | ||
std::vector<T0> operator+(std::vector<T1> const &a, std::vector<T2> const &b) { | ||
// 请实现列表的逐元素加法!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> | ||
std::ostream &operator<<(std::ostream &os, std::variant<T1, T2> const &a) { | ||
// 请实现自动匹配容器中具体类型的打印!10 分 | ||
} | ||
|
||
int main() { | ||
printf("Hello, world!\n"); | ||
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; | ||
} |