跳到正文

个人记录

代码规范

KAMIYA_KINA约 11 分钟
原文

这是 2026年3月1日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2021年10月27日
收录
2026年3月1日
更新
2026年11月9日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

Tag

代码习惯。

Preface

之前有一段时间代码习惯真的是巨差无比,各种压行,真的丑,后来掰过来之后觉得自己应该记录一下自己写代码的习惯性问题了,同时也是写各种文章的习惯也会在这里提及。

可能以后会有更新。

Main

头文件规范

  1. 可以使用 #include <bits/stdc++.h>。

  2. 在 #include 后一定要打空格。

  3. 最好不使用万能头文件。

  4. 可以使用 using namespace std;

宏定义规范

尽量不使用宏定义!

  • 可以使用:
C++
using mxq = priority_queue<int>;
using mnq = priority_queue<int, vector<int>, greater<int> >;
typedef long long ll;
typedef long double ld;
  • 不准使用:
C++
#define int long long
#define N 100001
  • 尽量不使用:
C++
#define FOR(i,a,b) for(int i=(a),i##i=(b);i<=i##i;i++)
#define ROF(i,a,b) for(int i=(a),i##i=(b);i>=i##i;i--)

全局变量规范

  • 在不冲突库函数的情况下,任何变量命名(最好是名词,并且可以表示变量含义)都可以使用,尽量用题面中给定的变量来命名某一个变量。

  • 避免 tot1, tot2, tot3, tot4 这样的情况产生。

cnt, num, tem 同理。

  • 避免已定义的全局变量在局部函数中重定义,如:
C++
int n, m;
void solve() {
  int n;
  cin >> n;
  cout << n << '\n';
}
  • 使用 const 来表示不变量。
C++
const int N = 1e5 + 1;
const int mod = 998244353;

函数有关规范

  • 函数尽量用动词缩写来描述,如:
C++
void solve();
void input();
void work();
void print();
  • 函数引用变量最好附上初值:
C++
void dfs(int step = 0, int ans = 0);
  • 函数一定要 return ;,哪怕是 void。

  • 函数一定实现在主函数前面。

  • 主函数里少放东西。

细节规定

  1. 在 , 和 ; 后面一定打空格,在运算符左右都要打空格。

  2. 能对齐且不影响整体美观的 ; 一定要对齐。

能对齐:

C++
for(int i = 1; i <= n; i++) cin >> a[i].lef;
for(int i = 1; i <= n; i++) cin >> a[i].rig;
for(int i = 1; i <= n; i++) cin >> a[i].id ;

int meg(int x, int y) {
    if(!x | !y) return x | y;
    return pr[x] > pr[y] ? 
    (rs[x] = meg(rs[x], y), psu(x), x):
    (ls[y] = meg(x, ls[y]), psu(y), y);
}

影响整体美观:

C++
for(int i = 1; i <= n; i++)
    cin >> a[i]
        >> b[i]
        >> c[i]
        >> d[i];

这巨丑无比兄弟。

  1. 大括号不换行。
C++
for(int i = 1; i <= n; i++) 
{
  //这什么坏文明
}
  1. 在右括号右边如果有大括号记得打空格,结构体和 namespace 一样。
C++
for(int i = 1; i <= n; i++) {

}

struct node {

};

namespace solve {


}
  1. 单行长度不超过 8080。

我的古老码风中最让我血压升高的:

C++
inline void add(int u, int v, int z) {nxt[++tot] = hd[u], to[tot] = v, val[tot] = z, hd[u] = tot;}
  1. 在意义相似的运算表达时打逗号,其他时候打分号换行。

  2. 输出使用 cout,迫不得已时可以使用 printf。

  3. 函数与函数之间要空一行。

C++
inline void input();

inline void work();

inline void solve();
  1. 变量分类管理,数组写一行,单个变量写一行。

如:

C++
int n, m, tot, cnt;
int hd[N], nxt[M], to[M], val[M];
char s[N], t[N];
  1. 模板所需要的数组要么放到结构体里,要么放在模板那里,不要放在题目所需的全局变量中。

如:

C++
int fa[N];
int fst(int x) {return fa[x] == x ? x : fa[x] = fst(fa[x]);}
bool ust(int x, int y) {
    x = fst(x), y = fst(y);
    if(x == y) return false;
    return fa[x] = y, true;
}

补充:函数代码较短可以压成一行,但是不能超过 8080 个字节。

  1. #define,#ifdef 都必须顶头,不能缩进。
C++
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    solve();
}
  1. 不使用 tab 符。

特殊说明

以上只是普通情况下的代码规范,在写模板时可以用熟悉的或者快速的写法。

(例如我的 LCT 模板和 Poly 模板)

Sample

  • 练习模板
C++
#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a),i##i=(b);i<=i##i;i++)
#define ROF(i,a,b) for(int i=(a),i##i=(b);i>=i##i;i--)

template<typename T> inline void cmin(T &x, T y) {x = (x < y ? x : y);}
template<typename T> inline void cmax(T &x, T y) {x = (x > y ? x : y);}

using ll = long long;
using ld = double;
using ull = unsigned long long;
using poly = vector<int>;

#ifdef LOCAL
#define dbg(x) cerr << #x " = " << x << '\n'
#define debug( ... ) fprintf(stderr, __VA_ARGS__)
#else 
#define dbg(x)
#define debug( ... )
#endif

namespace fastio {
#define gc (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
  char buf[100000], *p1(buf), *p2(buf);
  inline ll read() {
    ll x = 0;
    bool f = true;
    char ch = gc;
    while(!isdigit(ch)) {
      if(ch == '-') f = false;
      ch = gc;
    }
    while(isdigit(ch)) {
      x = (x << 1) + (x << 3) + (ch ^ 48);
      ch = gc;
    }
    return f ? x : -x;
  }
}
#define rd fastio::read()

inline void solve() {

}

int main() {
#ifdef LOCAL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  solve();
#ifdef LOCAL
  debug("%f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}
  • 考场模板
C++
#include <bits/stdc++.h>

using namespace std;

#define gc (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
char buf[100000], *p1(buf), *p2(buf);
#define rd read()
inline int read() {
    int x = 0, f = 1;
    char ch = gc;
    while(!isdigit(ch)) {
        if(ch == '-') f = 0;
        ch = gc;
    }
    while(isdigit(ch)) {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = gc;
    }
    return f ? x : -x;
}

inline void input() {
    
    
    return ;
}

inline void init() {
    
    
    return ;
}

inline void work() {
    
    
    return ;
}

inline void solve() {
    input();
    init();
    work();
    return ;
}

#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)

int main() {
    File("");
    solve();
    return 0;
}
  • 数据生成器
C++
#include <bits/stdc++.h>
#include <time.h>

using namespace std;

inline int Rnd(const int lef, const int rig) {
    int siz = (rig - lef + 1);
    return rand() * rand() % siz * rand() % siz + lef;
}

int main() {
    srand(time(NULL));
    return 0;
}
  • 对拍程序
C++
#include <bits/stdc++.h>

using namespace std;

#define pro ""

int main() {
    while(true) {
        system("make.exe > " pro ".in");
        system(pro ".exe");
        system("pai.exe < " pro ".in > pai.out");
        if(system("fc " pro ".out pai.out")) break;
    }
    system("pause");
    return 0;
}