# C++STL printer **Repository Path**: chiakimio/c-stl-printer ## Basic Information - **Project Name**: C++STL printer - **Description**: c++ 对拍板子,以及 debug 板子。 封装竞赛或算法练习时各种 c++ STL 的打印方法,可用于 debug 调试 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2022-01-21 - **Last Updated**: 2022-08-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 说明 * `leetcode`和`codeforces`等平台打印C++容器的代码,方便debug。 * 因为不会写可变参数所以没弄同时传参给`print`来打印多个容器的功能,所以请不要这样调用: ```c++ print(v1, v2, v3); ``` * 同时新增了个 bat 对拍脚本,只需要找一个文件夹放入三个文件: test.cpp, std.cpp, rnd.cpp,分别是测试文件,标解程序,随机数生成器程序。然后修改文件路径和 g++ 编译参数后即可使用,可以直接双击启动或者在命令行中输入如下命令,传入相应参数就可以额外对文件进行编译 ```bash check.bat # 默认启动,不进行编译 check.bat test # 对 test.cpp 进行编译 check.bat std # 对 std.cpp 进行编译 check.bat rnd test # 对 rnd.cpp, test.cpp 进行编译,顺序不影响 check.bat std test rnd # 对 std.cpp, test.cpp, rnd.cpp 三个文件都进行编译,顺序不影响 ``` # 使用 在debug的时候可以用如下代码进行日志打印 ```c++ // printf("%d %d %s\n", a, b, c); // 用下面的代替: debug("%d %d %s\n", a, b, c); // 打印stl容器: vector v = {1, 2, 3}; print(v); // 默认打印到结束后不会额外换行 print(v, 2); // 打印完vector的内容之后再额外换行两次 // 单独打印换行, 效果同printf("\n"); // 传入参数为换行次数,不传默认为1 enter(); enter(1); enter(2); // 按二进制打印数字 x printBit(x); // x=6, 打印 00110 (默认长度是5位) printBit(7, 10); // 打印长度为 10 位的 0000000111 ``` 在main函数上面进行了DEBUG宏定义: ```c++ #define DEBUG_ // 提交代码时注释掉这一行 #ifdef DEBUG_ #define debug(...) printf(__VA_ARGS__) #define print(...) print(#__VA_ARGS__, __VA_ARGS__) #define enter(x) _enter(x) #else #define print(...) 42 #define enter(x) 42 #define debug(...) 42 #endif ``` 在提交代码的时候,可以直接注释掉 `#define DEBUG_`,这样所有`print`, `enter`, `debug`的地方全部都会无效,不会打印出额外信息浪费时间或者造成`wrong answer`.