我的C++编码
一、秋招编码注意事项
所有输入输出,都自行采用ACM类型,绝不写单组输入输出!
比如,华为这题,用单组输入输出会出错,但是多组就能AC:传送门
二、关于cin和ios::sync_with_stdio(false)
使用这个的效果和scanf对比:传送门
最近做到一道题目,用ios::sync_with_stdio(false)和cin是TLE,于是把cin改成scanf(忘了删上面那句话)是WA,删了这句话就AC了。
用这个没什么坏处,就是不要和scanf混用吧,会造成异常,还有这句话虽然是加速了cin速度,但是还是没有scanf速度快,就这样
总结:
- 1.写了ios::sync_with_stdio(false)就不要使用scanf和printf了
- 2.写
代码样例
#include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int a,b; while(cin>>a>>b) { cout<<a+b<<endl; } return 0; }
三、关于GCC的attribute(constructor)代码,在main函数之前运行
__attribute__((constructor)) // 在main函数被调用之前调用 __attribute__((destructor)) // 在main函数被调用之后调
#include<stdio.h> __attribute__((constructor)) void before_main() { printf("before main\n"); } __attribute__((destructor)) void after_main() { printf("after main\n"); } int main(int argc, char **argv) { printf("in main\n"); return 0; }
GNU C 的一大特色就是attribute 机制。
attribute 可以设置函数属性(Function Attribute )、变量属性(Variable Attribute )和类型属性(Type Attribute )。
attribute 书写特征是:attribute 前后都有两个下划线,并切后面会紧跟一对原括弧,括弧里面是相应的attribute 参数。
attribute 语法格式为:attribute ((attribute-list))
attribute((constructor))
三、测试
codeforces上不去的话可以用镜像站:
codeforc.es
codeforces.ml