cin,cout之所以效率低,是因为先把要输出的东西存入缓冲区,再输出,导致效率降低.
在调用下列代码后,效率与scanf与printf相差无几,但是不能再用scanf了

[tips] [c++]
1
2
ios::sync_with_stdio(false);
cin.tie(0);

多层循环提高效率

[tips] [c++]
1
register int

auto的原理就是根据后面的值,来自己推测前面的类型是什么
针对迭代器 可以如下操作

[tips] [c++]
1
2
3
4
5
6
7
8
vector<string> ve;
ve.push_back(1);
ve.push_back(2);
ve.push_back(3);

for(auto i:ve) cout<<i<<endl;


快读 字符串输入 再转为数字 超级快

[int快读] [c++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int read()
{
int s=0,w=1;
char ch=getchar();
if(ch=='-') //先判断是不是个负数
{
w=-1;
ch=getchar();继续读下一个字符
}
while(ch>='0'&&ch<=9)
{
s=s*10+ch-'0';极其重要 记得-0
ch=getchar();
}
return w*s;
}

[double快读] [c++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
double read()
{
double s=0,w=1;
char ch=getchar();
if(ch=='-')
{
w=-1;
ch=getchar();
}
while(ch>='0'&&ch<=9)
{
s=s*10+ch-'0';
ch=getchar();前面都一样
}
if(ch=='.')
{
ch=getchar();
double f=0;
int i=0;//计算小数尾数
while(ch>='0'&&ch<='9')
{
f=f*10+ch-'0';
i++;
ch=getchar();
}
for(int j=0;j<i;j++)
{
f/=10;
}
s+=f
}
return w*s;
}
[int 快写] [c++]
1
2
3
4
5
void write(int x) {
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}