代码习惯

写在前面

此文用于记载本人的代码习惯。

变量&函数

基本准则

同类(同功能块)变量放在一起,在此前提下数组和数组放在一起。变量和函数分开。

int n,m,e,ans;
int cnt;
int head[N];
struct node{
	int to,next;
} edge[N*N];
int vis[N],mch[N];

inline void add(...){...}
void dfs(...){
    ...
}

最前面为最基本的输入输出,中间为前向星,结尾为匈牙利。函数同理。

结构体声明

构造函数&析构函数

不使用析构函数,不使用拷贝构造函数。

struct node{
    int l,r;
    node():l(0),r(0){}
    node(int _l,int _r):l(_l),r(_r){}
}

重载运算符

尽量使用友元函数。

friend bool operator<(const node &x,const node &y){
    return x.l<y.l;
}

数据结构封装

大型数据结构进行封装,小型则不。

int f[N];
int find(...){...}
void merge(...){...}

struct FHQ{
    ...
} Tree;

函数&声明

inline

只有反复使用且长度较短的函数才使用inline,其余不加。

inline void add(...){...}
void dfs(...){
    ...
}

模板函数

如输入输出使用模板函数,其他不使用。

template<typename T>inline void read(T &x){
    ...
}
template<typename T>inline void write(T x){
    ...
}
posted @ 2019-11-15 11:55  Ilverene  阅读(302)  评论(0编辑  收藏  举报