Skip to content

Commit 73734ed

Browse files
committed
init
1 parent f8fac1c commit 73734ed

File tree

3 files changed

+82
-17
lines changed

3 files changed

+82
-17
lines changed

README.md

+2-15
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,14 @@
1616

1717
## 评分规则
1818

19-
- 完成作业基本要求 50 分(详见下方"作业要求"
19+
- 完成作业基本要求 50 分(作业要求详见作业代码
2020
- 能够在 PR 描述中用自己的话解释 25 分
2121
- 代码格式规范、能够跨平台 5 分
2222
- 有自己独特的创新点 20 分
2323
- 明显抄袭现象 -100 分
2424

25-
## 作业要求
26-
27-
修改 main.cpp,改良其中的双链表类 `List`
28-
29-
- 避免函数参数不必要的拷贝 5 分
30-
- 修复智能指针造成的问题 10 分
31-
- 改用 `unique_ptr<Node>` 10 分
32-
- 实现拷贝构造函数为深拷贝 15 分
33-
- 说明为什么可以删除拷贝赋值函数 5 分
34-
- 改进 `Node` 的构造函数 5 分
35-
36-
并通过 `main()` 函数中的基本测试。
37-
3825
## 关于内卷
3926

40-
如果你把 List 改成了基于迭代器的,或是作为模板 `List<int>`
27+
如果你把 variant 的 operator<< 改成了基于变长模板参数的,或是实现了其他运算符
4128
只要是在 **满足作业要求的基础** 上,这是件好事!
4229
老师会酌情加分,视为“独特的创新点”,但最多不超过 20 分。

cpp_type_name.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <cstdlib>
5+
#if defined(__GNUC__) || defined(__clang__)
6+
#include <cxxabi.h>
7+
#endif
8+
9+
template <class T>
10+
std::string cpp_type_name() {
11+
const char *name = typeid(T).name();
12+
#if defined(__GNUC__) || defined(__clang__)
13+
int status;
14+
char *p = abi::__cxa_demangle(name, 0, 0, &status);
15+
std::string s = p;
16+
std::free(p);
17+
#else
18+
std::string s = name;
19+
#endif
20+
if (std::is_const_v<std::remove_reference_t<T>>)
21+
s += " const";
22+
if (std::is_volatile_v<std::remove_reference_t<T>>)
23+
s += " volatile";
24+
if (std::is_lvalue_reference_v<T>)
25+
s += " &";
26+
if (std::is_rvalue_reference_v<T>)
27+
s += " &&";
28+
return s;
29+
}

main.cpp

+51-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
1-
#include <cstdio>
1+
#include <iostream>
2+
#include <vector>
3+
#include <variant>
4+
5+
// 请修复这个函数的定义:10 分
6+
std::ostream &operator<<(std::ostream &os, std::vector<T> const &a) {
7+
os << "{";
8+
for (size_t i = 0; i < a.size(); i++) {
9+
os << a[i];
10+
if (i != a.size() - 1)
11+
os << ", ";
12+
}
13+
os << "}";
14+
return os;
15+
}
16+
17+
// 请修复这个函数的定义:10 分
18+
template <class T1, class T2>
19+
std::vector<T0> operator+(std::vector<T1> const &a, std::vector<T2> const &b) {
20+
// 请实现列表的逐元素加法!10 分
21+
// 例如 {1, 2} + {3, 4} = {4, 6}
22+
}
23+
24+
template <class T1, class T2>
25+
std::variant<T1, T2> operator+(std::variant<T1, T2> const &a, std::variant<T1, T2> const &b) {
26+
// 请实现自动匹配容器中具体类型的加法!10 分
27+
}
28+
29+
template <class T1, class T2>
30+
std::ostream &operator<<(std::ostream &os, std::variant<T1, T2> const &a) {
31+
// 请实现自动匹配容器中具体类型的打印!10 分
32+
}
233

334
int main() {
4-
printf("Hello, world!\n");
35+
std::vector<int> a = {1, 4, 2, 8, 5, 7};
36+
std::cout << a << std::endl;
37+
std::vector<double> b = {3.14, 2.718, 0.618};
38+
std::cout << b << std::endl;
39+
auto c = a + b;
40+
41+
// 应该输出 1
42+
std::cout << std::is_same_v<decltype(c), std::vector<double>> << std::endl;
43+
44+
// 应该输出 {4.14, 6.718, 2.618}
45+
std::cout << c << std::endl;
46+
47+
std::variant<std::vector<int>, std::vector<double>> d = c;
48+
std::variant<std::vector<int>, std::vector<double>> e = a;
49+
d = d + c + e;
50+
51+
// 应该输出 {9.28, 17.436, 7.236}
52+
std::cout << d << std::endl;
53+
554
return 0;
655
}

0 commit comments

Comments
 (0)