码风配置

· · 个人记录

理念

优先级从高到低依次为:规范、高效、抽象。(诶感觉这个好像优先级最高……)向 C++ 现代化语言靠齐(去 C 化,如使用 std::istringstream(argv[1]) 而不是 sscanf(argv[1])),大致向 Google 靠齐。

零散

禁用 using namespace std;,'<cxxx>' 库函数 强制使用 std:: 前缀。

禁止 bool 和 int 混用。bool 变量赋值 必须用 true 或 false。

前置 自增自减,尽量 不使用后置。

实测 STL 不慢。对于常用函数,尽量 使用 STL。

对于操作类型,全部 使用 switch。

一行最多 80 列,达到必须换行。

标准变量类型名,只用 using,其他缩写同理:

初始化方法优先级:memset / memcpy > fill / 函数 / 手写 > 构造函数。

#include 顺序:C++ 库 > C 库,按首字母排序。

位运算 非必要不加括号。

强转 必须 使用 1ll *、1. *。如遇到 __int128 等的强转(如巴雷特模乘),必须 使用 (i28)x 的形式。注意 std::decay_t<decltype(a)> b 的使用,以及和 std::decay 的区别。

namespace 右大括号后标注 // namespace XXX。

template 强制 换行,且应使用 class;

缩进

全局采用 3 tab 空格 缩进。

特殊情况:

  1. namespace 及 #if 内 不缩进,#if 等顶格写;
  2. 括号中内容过多,尽量从左括号行开始写,逗号或运算符 放末尾 隔开左右括号,输入输出流除外;或 换行 后按照原对齐 三格缩进。同行变量数不限制;
  3. 三目运算符,若换行则同时将 ? 和 : 换行并 三格缩进;
  4. switch 中 case 0: 等使用 一格缩进,且 不打大括号,除非限制变量作用域;
  5. class 中 public: 等使用 一格缩进;
  6. 如果有 初行不空行 的情况,其他行与 第一行左对齐,如输出函数。此时 要求 用于封闭的括号 不换行;
  7. 多行的 if 和 while 遵循 第 6 点;
  8. 其他可以遵循 Vim 自动缩进。

空格

空行

通常对于两个部分,意义不同 则空行。不出现 连续超过两行空行。大括号内部 不空行,public: 等 空行。#include、#define、namespace 中间空行。

通常空行分区为:定义、输入、预处理、核心、答案。

压行

直接 推荐 压行。

对于简单 if 语句,可以使用 短路机制 代替。由于测试发现不会减少代码量,同时预测分支常数玄学,酌情处理。

语句

if:

if (/* cond1 */) {
} else if (/* cond2 */) {
} else {
}

对于简单 check,可 原地定义并调用 lambda 函数

for:

for (int i = 0; ; ++i) {
}
for (auto y : G[x]) {
}

while:

while () {
}
do {
} while ();

switch:

switch (opt) {
 case 1:
   // do 1
   break;

 case 2:
   // do 2
   break;
}

禁用 default。

goto:

int main() {
   for (int i = 1; i <= n; ++i)
      for (int j = 1; j <= m; ++j)
         if (a[i][j] < 0) {
            goto _1;
         }
_1:write(x);
}

标签 顶格写,下划线加数字命名。

命名

常量 尽量 使用 constexpr,全大写 并用 下划线 分割单词。常用:N 默认数组大小(其次为 M、K);V 值域大小;P 模数;T 分块大小。

数组大小赋值,三位数以上使用 XeX + 5 的形式,三位数以下使用 XXX + 5 的形式,前面统一上取整。

数据组数用 _T,使用格式(编号同理用 _ID):

for (int _T = read<int>(); _T; --_T) work();
for (std::cin >> _T; _T; --_T) work();

大写驼峰式 用于 namespace、struct、class;小写驼峰式 用于变量名;蛇形命名法 用于函数名。小写驼峰式 可以 使用缩写,大写驼峰式则 不建议。注意特例如 void DFS_dfn();。封装时,class private: 中 统一 下划线开头,定义结构体及结构体内部 正常命名。

单个 _ 用于 Flush 类,单个 $ 用于 Init 类。

函数返回值 应 为 ret 而不是 res。

DP 数组尽量不用 dp。

使用的变量名较长时,可以考虑用 int &。

封装

数据结构 用 class 封装,算法(or Subtask)用 namespace 封装,其余用 struct 分明变量,用 pair、tuple 代替 当且仅当 不细节区分内容。

构造函数中,冒号 前空格,并 完全换行,优先使用冒号赋值;如写构造函数,必须 令 默认构造函数 为 default。

快读、数学模板(如快速幂、\gcd),函数名统一加 inline。

暂时禁用 匿名结构体。

const & 使用当且仅当传入值不是基本数据类型,否则效率会降低。(正确:std::pair<int, int>。)

Splay 内用 struct 封装节点,LCT 不封装。

特殊的,求 LCA 用的 ST 表不封装。

重载过程中,尽量 不用 friend,但是 矩阵乘法,有方向性的线段树信息 必须用 friend 重载。

标准封装名:

class:

struct:

特殊的,zkw 线段树 中 不 封装节点,_m 表示树的大小,_dep 表示树的深度,_val 表示节点信息,_tag 表示标记相关。

调试

顶格 #if DEBUG 封起来,与上下换行。标准使用 std::cerr 以及 std::endl。

模板

现行标准头文件模板(为 101 行)如下:

/* 年挽红枫,溪傍芦荻。*/

#ifdef DEBUG
#include <iostream>
#include <cmath>
#include <ctime>

bool Mbe;
void _Dihan();
#endif

#include <cstdio>
#include <cctype>

typedef long long i64;
typedef unsigned long long ull;
typedef double f32;
typedef long double ldb;
typedef __int128 i28;
typedef unsigned uit;

template <typename T>
inline bool chkMx(T &x, T y) { return x < y ? x = y, true : false; }
template <typename T>
inline bool chkMn(T &x, T y) { return x > y ? x = y, true : false; }

namespace IO {

#define file(s) freopen(#s".in", "r", stdin), freopen(#s".out", "w", stdout)

constexpr int SIZE = 1 << 21;

char ibuf[SIZE], *p1 = ibuf, *p2 = ibuf, obuf[SIZE], *p3 = obuf;

#define flush() (fwrite(obuf, 1, p3 - obuf, stdout), p3 = obuf)
#define gc()                                                               \
   (p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, SIZE, stdin), p1 == p2) \
       ? EOF                                                               \
       : *p1++)
#define pc(ch) (p3 == obuf + SIZE && flush(), *p3++ = ch)

class Flush { public: ~Flush() { flush(); } } _;

template <typename T>
inline void read(T &x) {
   static char c;
   static int f;

   x = f = 0;
   while (!isdigit(c = gc())) f |= c == '-';

   while (isdigit(c)) x = x * 10 + (c ^ 48), c = gc();
   f && (x = -x);
}
template <typename T>
inline T read() {
   static T x;

   return read(x), x;
}

template <typename T>
inline void write(T x) {
   static char s[50];
   static int t;

   x < 0 && (pc('-'), x = -x);
   do s[++t] = x % 10 ^ 48; while (x /= 10);

   while (t) pc(s[t--]);
}
inline void write(char c) { pc(c); }
template <typename T>
inline void write(T *s) { while (*s) pc(*s++); }

template <typename ...Args>
inline void read(Args &...args) { (read(args), ...); }
template <typename ...Args>
inline void write(Args ...args) { (write(args), ...); }

}  // namespace IO
using namespace IO;

int main() {
#ifdef DEBUG
   file(cur);
#endif

#ifdef DEBUG
   _Dihan();
#endif
   return 0;
}

#ifdef DEBUG
bool Med;
void _Dihan() {
   std::cerr << "Memory: " << abs(&Med - &Mbe) / 1048576.0 << " MB\n";
   std::cerr << "Time: " << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
}
#endif