Merge pull request #2 from ChungZH/patch-1

Add dbg-marco and fix typo
This commit is contained in:
Francis 2020-03-04 09:44:16 +08:00 committed by GitHub
commit f22bdab57a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 120 additions and 48 deletions

View File

@ -1,7 +1,7 @@
# C++ 那些事
感谢各位对《C++那些事》的支持,现将内容也同步至网站,可以手机或者网站直接阅读~欢迎大家star转发pr
感谢各位对《C++ 那些事》的支持,现将内容也同步至网站,可以打开网站直接阅读~欢迎大家 star、转发、PR
[直通点](https://light-city.club/sc/)
@ -49,7 +49,7 @@
#### 2.0 概况
C++2.0简称包括C++11/14/17/20。
C++2.0 是一个简称,意为「现代 C++」,包括 C++11/14/17/20。
#### 2.1 [C++11 新特性](./c++2.0/c++11)
@ -110,9 +110,9 @@ C++2.0简称包括C++11/14/17/20。
- 容器-结构与分类
- (1) 序列式容器包括array(C++2.0新引入),vector,deque,list,forward_list(C++2.0新引入)
- (2) 关联式容器包括set/multiset,map/multimap
- (3) 无序容器(C++2.0新引入,更换原先hash_xxx为unordered_xxx)包括unordered_map/unordered_multimap,unordered_set/unordered_multiset
- (1) 序列式容器包括:`array`(C++2.0 新引入),`vector`,`deque`,`list`,`forward_list`(C++2.0 新引入)
- (2) 关联式容器包括:`set/multiset`,`map/multimap`
- (3) 无序容器(C++2.0 新引入,更换原先 `hash_xxx` `unordered_xxx`)包括:`unordered_map/unordered_multimap,unordered_set/unordered_multiset`
- [Hash Function](./c++2.0/hash.cpp)
@ -130,7 +130,7 @@ C++2.0简称包括C++11/14/17/20。
### 4. [STL 源码剖析](./src_analysis/stl)
**stl源码剖析gcc4.9.1**
**STL 源码剖析gcc 4.9.1**
- [array](./src_analysis/stl/array.md)
- [deque](./src_analysis/stl/deque.md)
@ -232,7 +232,7 @@ map<int, int> mp{
{ 1 => 1, 2 => 4, 3 => 9 }
```
#### 7.2 像Python一样简单输出
#### 7.2 像 Python 一样简单输出Jupyter Notebook
- [像 Python 一样玩 C/C++](./tool/像Python一样玩CC++.md)
@ -240,6 +240,10 @@ map<int, int> mp{
- [https://cppinsights.io](https://cppinsights.io/)
#### 7.4 C++ 的 Debug 工具 dbg-macro
- [C++ 的 Debug 工具 dbg-macro](./tool/C++的Debug工具dbg-macro.md)
### 8.拓展部分
#### 8.1 一些问题

View File

@ -0,0 +1,68 @@
# C++ 的 debug 工具dbg-macro
打日志是 C++ 开发中必不可少的一种 debug 方式dbg-macro 受 Rust 语言中 的 `dbg` 启发,提供比 `printf``std::cout` 更好的宏函数。主要有如下特点:
- 美观的彩色输出(当输出不是交互式终端时,颜色将自动禁用)
- 兼容 C++11并且是 header-only
- 支持基础类型和 STL 容器类型的输出
- 可以输出文件名、行号、函数名和原始表达式
例如下面的代码:
```cpp
#include <vector>
#include <dbg.h>
// You can use "dbg(..)" in expressions:
int factorial(int n) {
if (dbg(n <= 1)) {
return dbg(1);
} else {
return dbg(n * factorial(n - 1));
}
}
int main() {
std::string message = "hello";
dbg(message); // [example.cpp:15 (main)] message = "hello" (std::string)
const int a = 2;
const int b = dbg(3 * a) + 1; // [example.cpp:18 (main)] 3 * a = 6 (int)
std::vector<int> numbers{b, 13, 42};
dbg(numbers); // [example.cpp:21 (main)] numbers = {7, 13, 42} (size: 3) (std::vector<int>)
dbg("this line is executed"); // [example.cpp:23 (main)] this line is executed
factorial(4);
return 0;
}
```
效果如下:
![](https://camo.githubusercontent.com/3cde47c8db560b3ed42763f2ef306d78d4b19087/68747470733a2f2f692e696d6775722e636f6d2f4e4845596b39412e706e67)
## 安装
```bash
git clone https://github.com/sharkdp/dbg-macro
sudo ln -s $(readlink -f dbg-macro/dbg.h) /usr/include/dbg.h
```
这样,你就可以在任何地方使用 `dbg.h` 了。
### 在 Arch Linux 上
```bash
yay -S dbg-macro
```
### 使用 vcpkg
```bash
vcpkg install dbg-macro
```
更多内容详见 [官方文档](https://github.com/sharkdp/dbg-macro)。