From 97240276a7941943a05a057f91430caca0d97612 Mon Sep 17 00:00:00 2001 From: Jacob Zhong Date: Fri, 6 Sep 2019 11:13:34 -0400 Subject: [PATCH] Append review contents --- docs/lang/c-cpp.md | 20 +++++++++++++++++++- docs/lang/const.md | 10 ++++++---- docs/lang/csl/iterator.md | 2 +- docs/lang/op.md | 23 ++++++++++++++--------- docs/lang/python.md | 30 +++++++++++++++--------------- 5 files changed, 55 insertions(+), 30 deletions(-) diff --git a/docs/lang/c-cpp.md b/docs/lang/c-cpp.md index 8b20ec85..c9e86daa 100644 --- a/docs/lang/c-cpp.md +++ b/docs/lang/c-cpp.md @@ -10,7 +10,25 @@ C++ 中你仍然可以使用 C 风格的指针,但是对于变量传递而言 ## struct -尽管在 C 和 C++ 中都有 struct 的概念,但是他们对应的东西是不能混用的!C 中的 struct 用来描述一种固定的内存组织结构,而 C++ 中的 struct 就是一种类, **唯一的区别就是它的成员默认是 public 的** ,而一般类的默认成员是 private 的。这一点在写 C/C++ 混合代码时尤其致命。 +尽管在 C 和 C++ 中都有 struct 的概念,但是他们对应的东西是不能混用的!C 中的 struct 用来描述一种固定的内存组织结构,而 C++ 中的 struct 就是一种类, **它与类唯一的区别就是它的成员和继承行为默认是 public 的** ,而一般类的默认成员是 private 的。这一点在写 C/C++ 混合代码时尤其致命。 + +另外,声明 struct 时 C++ 也不需要像 C 那么繁琐,C 版本: + +```c +typedef struct Node_t{ + struct Node_t *next; + int key; +} Node; +``` +C++ 版本 + +```cpp +struct Node +{ + Node *next; + int key; +}; +``` ## const diff --git a/docs/lang/const.md b/docs/lang/const.md index 6de9ff11..1f40d9c6 100644 --- a/docs/lang/const.md +++ b/docs/lang/const.md @@ -6,14 +6,15 @@ C++ 定义了一套完善的只读量定义方法,被常量修饰符 `const` ### 常量 -这里的常量即常变量,指的是 const 变量(而不是标题里泛指的不变量)。const 变量在声明之后便不可重新赋值,也不可访问其可变成员,只能访问其常成员。常成员的定义见后文。 +这里的常量即常变量,指的是 const 类型的变量(而不是标题里泛指的只读量)。常类型量在声明之后便不可重新赋值,也不可访问其可变成员,只能访问其常成员。常成员的定义见后文。 ??? note "类型限定符" C++ 中类型限定符一共有三种:常量(const)、可变(mutable)和易变(volatile),其中默认情况下是可变变量,声明易变变量的情形是为了刻意避免编译器优化。 ```cpp const int a = 0; // a 的类型为 const int - // a = 1; // 报错,不能修改常量 + +// a = 1; // 报错,不能修改常量 ``` ### 常引用、常指针 @@ -76,8 +77,9 @@ struct X { X(); const int* p; // 类型为 int* 的常成员 int* const q; // 类型为 const int* 的可变成员 - const int r() - const; // 第一个 const 修饰返回值,而最后的 const 修饰的是这个成员函数。 + + const int r() const; + // 第一个 const 修饰返回值,而最后的 const 修饰的是这个成员函数。 }; X a; diff --git a/docs/lang/csl/iterator.md b/docs/lang/csl/iterator.md index 23a29013..7cc2aa61 100644 --- a/docs/lang/csl/iterator.md +++ b/docs/lang/csl/iterator.md @@ -2,7 +2,7 @@ ## 使用方法 -迭代器听起来比较晦涩,其实迭代器本身可以看作一个数据指针。迭代器主要有两个操作:自增和解引用( [单目 `*` 运算符](../op.md) ),其中自增用来移动迭代器,解引用可以获取或修改它指向的元素。 +迭代器听起来比较晦涩,其实迭代器本身可以看作一个数据指针。迭代器主要支持两个运算符:自增和解引用( 单目 `*` 运算符 ),其中自增用来移动迭代器,解引用可以获取或修改它指向的元素。 最常用的使用方法是用迭代器替换普通的 `for` 循环,例如下列代码中两个循环的效果是一致的。(假设已经引用了 `std` 空间中的相关类型) ```cpp diff --git a/docs/lang/op.md b/docs/lang/op.md index 47f9e3ff..3f0b9dd9 100644 --- a/docs/lang/op.md +++ b/docs/lang/op.md @@ -135,13 +135,18 @@ Result = (1 + 2, 3 + 4, 5 + 6); 这些运算符用来访问对象的成员或者内存,除了最后一个运算符外上述运算符都可被重载。与 `&` , `*` 和 `->` 相关的内容请阅读 [指针](./pointer.md) 和 [引用](./reference.md) 教程。这里还省略了两个很少用到的运算符 `.*` 和 `->*` ,其具体用法可以参见 [C++ 语言手册](https://zh.cppreference.com/w/cpp/language/operator_member_access) 。 ```cpp -auto result = v[1]; // 获取v中下标为2的对象 - -auto result = p.q; // 获取p对象的q成员 - -auto result = p -> q; // 获取p指针指向的对象的q成员,等价于 (*p).q - -auto result = &v; // 获取指向v的指针 - -auto result = *v; // 获取v指针指向的对象 +auto result1 = v[1]; // 获取v中下标为2的对象 +auto result2 = p.q; // 获取p对象的q成员 +auto result3 = p -> q; // 获取p指针指向的对象的q成员,等价于 (*p).q +auto result4 = &v; // 获取指向v的指针 +auto result5 = *v; // 获取v指针指向的对象 + +// 指针与引用相互转换 +int a = 1; +int* b = &a; // 这里的*不是运算符 +int& c = *b; // 这里的&不是运算符 +assert(b == &c); + +std::vector data {1,2}; +int* d = &data[0]; // 一种获取 vector 内存的方法 ``` diff --git a/docs/lang/python.md b/docs/lang/python.md index e2362a12..f4a1ac77 100644 --- a/docs/lang/python.md +++ b/docs/lang/python.md @@ -161,7 +161,7 @@ Python 以其简洁易懂的语法而出名。它基本的语法结构可以非 Python 中的输入输出主要通过内置函数 `raw_input` (Python 2)/ `input` (Python 3) 和 `print` 完成,这一部分内容可以参考 [Python 的官方文档](https://docs.python.org/3/tutorial/inputoutput.html) 。 `input` 函数用来从标准输入流中读取一行, `print` 则是向标准输出流中输出一行。在 Python 3 中对 `print` 增加了 `end` 参数指定结尾符,可以用来避免 `print` 自动换行。如果需要更灵活的输入输出操作,可以在引入 `sys` 包之后利用 `sys.stdin` 和 `sys.stdout` 操标准作输入输出流。 -另外,如果要进行格式化的输出的话可以利用 Python 中字符串的语法。格式化有两种方法,一种是利用 `%` 操作符,另一种是利用 `format` 函数。前者语法与 C 兼容,后者语法比较复杂,可以参考 [官方文档](https://docs.python.org/2/library/string.html#formatstrings) 。 +另外,如果要进行格式化的输出的话可以利用 Python 中字符串的语法。格式化有两种方法,一种是利用 `%` 操作符,另一种是利用 `format` 函数。前者语法与 C 兼容,后者语法比较复杂,可以参考 [官方文档](https://docs.python.org/3/library/string.html#formatstrings) 。 ```python3 >>> print(12) @@ -276,20 +276,20 @@ array([[0, 0, 1], | 包名 | 用途 | | --------------- | ---------------- | -| `array` | 定长数组 | -| `argparse` | 命令行参数处理 | -| `bisect` | 二分查找 | -| `collections` | 提供有序字典、双端队列等数据结构 | -| `fractions` | 有理数 | -| `heapq` | 基于堆的优先级队列 | -| `io` | 文件流、内存流 | -| `itertools` | 迭代器相关 | -| `math` | 常用数学函数 | -| `os.path` | 系统路径相关 | -| `random` | 随机数 | -| `re` | 正则表达式 | -| `struct` | 转换结构体和二进制数据 | -| `sys` | 系统信息 | +| [`array`](https://docs.python.org/3/library/array.html) | 定长数组 | +| [`argparse`](https://docs.python.org/3/library/argparse.html) | 命令行参数处理 | +| [`bisect`](https://docs.python.org/3/library/bisect.html) | 二分查找 | +| [`collections`](https://docs.python.org/3/library/collections.html) | 提供有序字典、双端队列等数据结构 | +| [`fractions`](https://docs.python.org/3/library/fractions.html) | 有理数 | +| [`heapq`](https://docs.python.org/3/library/heapq.html) | 基于堆的优先级队列 | +| [`io`](https://docs.python.org/3/library/io.html) | 文件流、内存流 | +| [`itertools`](https://docs.python.org/3/library/itertools.html) | 迭代器相关 | +| [`math`](https://docs.python.org/3/library/math.html) | 常用数学函数 | +| [`os.path`](https://docs.python.org/3/library/os.html) | 系统路径相关 | +| [`random`](https://docs.python.org/3/library/random.html) | 随机数 | +| [`re`](https://docs.python.org/3/library/re.html) | 正则表达式 | +| [`struct`](https://docs.python.org/3/library/struct.html) | 转换结构体和二进制数据 | +| [`sys`](https://docs.python.org/3/library/sys.html) | 系统信息 | ## 对比 C++ 与 Python -- 2.11.0