Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Dec 25, 2021
1 parent f8fac1c commit 73734ed
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 17 deletions.
17 changes: 2 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,14 @@

## 评分规则

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

## 作业要求

修改 main.cpp,改良其中的双链表类 `List`

- 避免函数参数不必要的拷贝 5 分
- 修复智能指针造成的问题 10 分
- 改用 `unique_ptr<Node>` 10 分
- 实现拷贝构造函数为深拷贝 15 分
- 说明为什么可以删除拷贝赋值函数 5 分
- 改进 `Node` 的构造函数 5 分

并通过 `main()` 函数中的基本测试。

## 关于内卷

如果你把 List 改成了基于迭代器的,或是作为模板 `List<int>`
如果你把 variant 的 operator<< 改成了基于变长模板参数的,或是实现了其他运算符
只要是在 **满足作业要求的基础** 上,这是件好事!
老师会酌情加分,视为“独特的创新点”,但最多不超过 20 分。
29 changes: 29 additions & 0 deletions cpp_type_name.h
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;
}
53 changes: 51 additions & 2 deletions main.cpp
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;
}

0 comments on commit 73734ed

Please sign in to comment.