跳到正文

个人记录

【个人记录 #1】个人代码规范

mofan101028约 3 分钟
原文

这是 2025年12月22日 存档的第 7 版,不是当前版本。查看当前版本

文章信息与版本

前言

这些都是按照自己的习惯写的,代码风格因人而异。

板子

C++
#include <bits/stdc++.h>
using namespace std;
#define upp(x,y,z) for(x=(y);x<=(z);++x)
#define low(x,y,z) for(x=(y);x>=(z);--x)
#define tt template<typename T>
#define fs(x) fixed<<setprecision(x)
#define ll long long
#define ull unsigned long long
#define debug(x) cout<<"Line "<<__LINE__<<","<<(x)<<"."<<endl
tt inline void chkmax(T &x,T y){if(x<y){x=y;}}
tt inline void chkmin(T &x,T y){if(x>y){x=y;}}
const int eps=0xf;const int inf=0x7fffffff;const ll infll=0x7fffffffffffffff;const ull infull=0xffffffffffffffff;
const int N=1e6+eps;
bool memorybegin;

void code(){
	//do something...
}
bool memoryend; 
int main(){
	#define DEBUG mofan101028
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); code();
	#ifdef DEBUG
	cout<<"CPU time used:"<<fs(3)<<(double)clock()/CLOCKS_PER_SEC<<"s."<<endl;
	cout<<"Memory used:"<<fs(3)<<abs((&memoryend-&memorybegin)*1.0/1024.0/1024.0)<<"MB."<<endl;
	#endif
	return 0;
}

板子随时调整。

规范

  • 要尽量压行,每个代码块之间空一个空格。
例子

要尽量写成:

C++
int a,b; cin>>a; cin>>b; cout<<a+b<<endl;

而不要写成:

C++
int a,b;
cin>>a;
cin>>b;
cout<<a+b<<endl;

暂且不管板子中 const\text{const} 那一行。

  • 括号里内容比较少就不换行。
例子

如板子中的 chkmax\text{chkmax} 和 chkmin\text{chkmin} 。