Skip to content

Commit

Permalink
update: cpp tutorial personal
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuka007 committed Oct 6, 2024
1 parent e63bd6b commit d2667d5
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/guide/tutorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ icon: fa-solid:chalkboard-teacher
这里存放一些教学资源与简介。

<Catalog />

<DiscourseComments />
4 changes: 3 additions & 1 deletion src/guide/tutorial/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ title: C++ 专题
icon: vscode-icons:file-type-cpp3
---

<Catalog />
<Catalog />

<DiscourseComments />
152 changes: 152 additions & 0 deletions src/guide/tutorial/cpp/book-read/1-helloworld.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
---
title: 1 - "Hello World!"
---

## 基本程序概念

- 红 P3-9 / OCR P23
- 蓝 P28-41

::: tip 读完本小结,你应该能够
:::

- 感性了解C++程序的基本结构
- 了解 [`预处理指令`](https://zh.cppreference.com/w/cpp/preprocessor)
- 初识 `#include <>``#include ""`
- 初识 `#define`
- 了解 [`注释`](https://zh.cppreference.com/w/cpp/comment) 的作用
- 了解 [`main函数`](https://zh.cppreference.com/w/cpp/language/main_function) 的作用
- 初识 [`命名空间`](https://zh.cppreference.com/w/cpp/language/namespace)
- 初识 [`输入输出流`](https://zh.cppreference.com/w/cpp/io) 也就是
- `#include <iostream>` 中的 [`iostream`](https://zh.cppreference.com/w/cpp/header/iostream) 头文件
- `std::cout`
- `std::cin`
- `<<``>>`,实际上是运算符

> 更推荐参考:1.1.1 5.输入输出流 C++20高级编程(第五版)P6 / OCR P26
- 了解 [`控制流`](https://zh.cppreference.com/w/cpp/language/statements)

::: tip 结合两本书,你还能了解到
:::

- 字符串里别扭的 [`转义字符`](https://zh.cppreference.com/w/cpp/language/escape)`\n` `\t`
- 认识基础[`运算符`],如 `+` `-` `*` `/` `%` `=` `==` `!=` `>` `<` `>=` `<=`
- 一些奇怪的名词,比如
- [`函数`](https://zh.cppreference.com/w/cpp/language/functions)
- [`字面量`](https://zh.cppreference.com/w/cpp/language/expressions#.E5.AD.97.E9.9D.A2.E9.87.8F)
- [`初始化`]

::: tip 以下内容会在后一节详细讲解
:::

- `变量`, 准确来说是 [`标识符`]
- [`基本类型`],后面还会学到 [``]
- [`类型`],实际上还会有另外一个名词 [`值类别`],这俩特别容易搞混
- [`声明`](https://zh.cppreference.com/w/cpp/language/declarations) - `标识符声明` 又名 `简单声明`

## 一系列帮助建立直观认识的例子

**hello world**
```cpp
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
}
```

**一个简单的加法**
```cpp
#include <iostream>
int main() {
int a = 1;
int b = 2;
int c = a + b;
std::cout << c << '\n';
std::cout << a + b << '\n';
}
```

**一个简单的判断**
```cpp
#include <iostream>
int main() {
int a = 1;
int b = 2;
if (a > b) {
std::cout << "a > b\n";
} else {
std::cout << "a <= b\n";
}
}
```

**一个简单的循环**
```cpp
#include <iostream>
int main() {
for (int i = 0; i < 10; ++i) {
std::cout << i << '\n';
}
}
```

**一个简单的函数**
```cpp
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
int a = 1;
int b = 2;
int c = add(a, b);
std::cout << c << '\n';
}
```
**一个简单的命名空间**
```cpp
#include <iostream>
namespace A {
int a = 1;
}
namespace B {
int a = 2;
}
int main() {
std::cout << A::a << '\n';
std::cout << B::a << '\n';
}
```

## 使用 Compiler Explorer

- https://godbolt.org/
- https://compiler-explorer.com/

::: tip
Compiler Explorer 是一个在线的编译器,可以帮助你快速查看代码的编译结果。

不过我目前没有精力一个一个敲出来生成短链给你们来尝试,你们可以自己打开,然后把上面的代码粘贴进去,
看看编译结果,以及有空的话,生成短链,粘贴到文章中,方便大家查看。
:::

::: warning
如果粘贴后只有汇编结果没有输出结果,可以如图勾上 Execute the code 并点击/拖拽下方 Output:
:::

![选中 Execute the code](exec.png)

![点击 或 拖拽 Output](exec2.png)

[`基本类型`]: https://zh.cppreference.com/w/cpp/language/types
[`类型`]: https://zh.cppreference.com/w/cpp/language/type
[`值类别`]: https://zh.cppreference.com/w/cpp/language/value_category
[`运算符`]: https://zh.cppreference.com/w/cpp/language/operators
[`表达式`]: https://zh.cppreference.com/w/cpp/language/expressions
[`初始化`]: https://zh.cppreference.com/w/cpp/language/initialization
[`类`]: https://zh.cppreference.com/w/cpp/language/classes
[`标识符`]: https://zh.cppreference.com/w/cpp/language/identifiers
[`对象`]: https://zh.cppreference.com/w/cpp/language/object

<DiscourseComments />
108 changes: 108 additions & 0 deletions src/guide/tutorial/cpp/book-read/2-variable-like.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
title: 2 - 各种变量一样的东西
---

::: warning
不完整,待补充
:::

## 基本类型 与 标识符

参考
- 红 P9 / OCR P29
- 蓝 P32

**关键名词**
- [`基本类型`]
- [`类型`]
- [`标识符`]

**C风格数组**
- 红 1.1.14 P26 / OCR P46

## 类 Class 与 结构体 Struct
事实上在编写 [``] 之前,我更推荐先使用一下 标准库 提供的 类:
- std::array 与 std::vector
- 红 1.1.15 P27 / OCR P47
> https://compiler-explorer.com/z/Menne4vEa
```cpp
#include <vector>
#include <iostream>
using namespace std;
int main() {
// Create a vector of Integers.
vector<int> myVector { 11,22 };
// Add some more integers to the vector using push_back.
myVector.push_back(33);
myVector.push_back(44);
// Access elements.
cout << "1st element " << myVector[0] << endl;
}
```

类的编写与进阶
- 蓝 第三章
- 红 P61、135、197 / OCR P81、155、217
> 在 C++20 高级编程(第五版)中,由于使用了 module 的编写规范,所以对于类型/标识符
等定义会带上 `export` 关键字,如果不启用 module,可以忽略 `export` 关键字。


**关键名词**
- [``]
- [`对象`]
- [`结构体`](https://zh.cppreference.com/w/cpp/language/struct)

::: warning 关于C++可能缺少的一课
以下内容可以跳过,因为可能涉及一系列关于 函数重载,类的复制、移动构造 等 进阶内容。
:::

## 联合体 Union
- [联合体](https://zh.cppreference.com/w/cpp/language/union)

## 类型推导

## 值类别
- [`值类别`]

以函数重载来举例,请问下列程序输出什么?为什么会有这样的区别?
> https://compiler-explorer.com/z/eGqPvTxMz
```cpp
#include <cstdio>
class X {};

void print(X& x) {
std::puts("X& x\n");
}

void print(X&& x) {
std::puts("X&& x\n");
}

X getX() {
return X{};
}

int main() {
X x;
print(x);
print(getX());
}
```
参考
- 白 1.1 PDF P10
## C++ 类型系统概览
![C++ 类型系统概览](https://upload.cppreference.com/mwiki/images/9/96/cpp_types.svg)
[`基本类型`]: https://zh.cppreference.com/w/cpp/language/types
[`类型`]: https://zh.cppreference.com/w/cpp/language/type
[`值类别`]: https://zh.cppreference.com/w/cpp/language/value_category
[`运算符`]: https://zh.cppreference.com/w/cpp/language/operators
[`表达式`]: https://zh.cppreference.com/w/cpp/language/expressions
[`初始化`]: https://zh.cppreference.com/w/cpp/language/initialization
[`类`]: https://zh.cppreference.com/w/cpp/language/classes
[`标识符`]: https://zh.cppreference.com/w/cpp/language/identifiers
[`对象`]: https://zh.cppreference.com/w/cpp/language/object
<DiscourseComments />
22 changes: 22 additions & 0 deletions src/guide/tutorial/cpp/book-read/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: 书籍阅读与顺序推荐
---

::: warning 本文仅供参考
仅代表个人学习经验。本文撰写者围绕“现代C++”,并秉持 ***写的代码应该直接就能被人读懂** 为第一原则,因此本教程也许不适合部分 `ACM` 竞赛选手等人群,请酌情参考。另一方面,出于对知识周延性的考虑,有时候可能会涉及重复的章节,以及对几本书的拆解与交叉引用。
:::

::: tip
本文中会根据专有名词嵌入 [`cppreference`](https://zh.cppreference.com/w/cpp) 的链接,伴随着蓝色字体与小箭头的超链接符号,方便读者查阅。如果你感兴趣,或者想要了解更多,欢迎点进去了解。不过,贪多嚼不烂,不能理解也无所谓,有时候cppreference也实在不讲人话。你只要记下有这个名词也无妨,只是你需要记得有这个东西。

**cppreference** 网址:https://zh.cppreference.com
:::

## 书籍推荐
- C++20高级编程(第五版),后面使用 红皮书 表示
- C++20高级编程(罗能),后面使用 白皮书 表示
- C++大学教程 第9版,后面使用 蓝皮书 表示

<Catalog />

<DiscourseComments />
Binary file added src/guide/tutorial/cpp/book-read/exec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/guide/tutorial/cpp/book-read/exec2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/guide/tutorial/cpp/mq-b.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ b站视频购买链接:https://b23.tv/Ppq4Bsw

> 搬运者注:loser homework是一个很有意思的项目,是一个C++的练习项目,里面有很多有趣的题目,可以帮助你提高C++编程能力。不过,大概只有我们loser才会专注于C++这些乱七八糟的东西吧。
- https://github.com/Mq-b/Loser-HomeWork
- https://github.com/Mq-b/Loser-HomeWork

<DiscourseComments />

0 comments on commit d2667d5

Please sign in to comment.