代码模板工具入口

代码模板工具入口

一句话工具

这个页面是工具模板的整理入口,用来说明通用竞赛模板和工具函数之间的关系。

为什么保留这个入口

本书里有两类代码:

  • 算法模板:例如树状数组、最短路、字符串匹配,通常放在对应算法文章里。
  • 工具模板:例如快读、调试输出、随机数据生成,平时写很多题都会用到。

工具模板不一定对应一个算法知识点,但它们会影响写题效率、调试效率和对拍效率。因此需要单独整理。

本页不在 front matter 中声明 code_template,是为了避免和附录里的“竞赛代码模板”重复出现在代码模板检索页。真正的通用模板入口在:

  • appendix/template/index.md

工具模板之间的关系

常用工具可以按使用阶段分成三类。

阶段 工具 作用
写题前 竞赛代码模板 提供基础骨架、调试宏、快读引用
写题中 调试输出工具 快速观察变量状态
验证时 随机数、随机图生成 生成对拍数据和压力测试数据

这些工具可以组合使用。例如通用模板里引用 quickIO,调试时使用 logdebug,写完后用随机生成器对拍。

通用模板

cpp
        
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/** * Author by {{author}} blog: {{blog}} github : {{github}} * date: {{date}} * oj: {{oj}} * title: {{title}} * description: {{description}} */ #include <bits/stdc++.h> using namespace std; typedef long long ll; // #define NO_DEBUG // switch debug #if defined(onlinejudge) || defined(ONLINE_JUDGE) || defined(NO_DEBUG) #define log(...) #define fenc #else #define log(args...) { cout << "LINE:" << __LINE__ << " : ";string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } #define fenc cout<<"================================"; void err(istream_iterator<string> it) {} template<typename T> void err(istream_iterator<string> it, T a) { cerr << *it << " = " << a << "\n"; } template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); } #endif const int maxn = 1e6+5; int n,m; int a[maxn]; {{include "code/utils/quick_io.cpp"}} void init() { read(n); CURRENT_LINE } signed main () { #ifdef FREOPEN freopen("in", "r",stdin); #endif // std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // 关闭io同步 return 0; }

推荐使用顺序

  1. 先从通用模板开始写题。
  2. 如果输入输出量很大,保留 quickIO;否则可以改成标准流。
  3. 本地调试时打开调试宏。
  4. 写完正解后,用随机数或随机图生成器做对拍。
  5. 提交前关闭调试输出,并检查模板里的占位符。

相关工具文章

维护原则

  • 正式算法文章需要模板时,应在对应算法教程的 code_template 中引用具体模板。
  • 通用工具代码应放在 book/code/utils/book/code/template/
  • Markdown 正文只解释用法,不粘贴整份可复用代码。
  • 如果某个工具已经有独立文章,不要在本页重复声明 code_template