std::ios::sync_with_stdio(false);

这句语句是用来取消cin的同步,什么叫同步呢?就是iostream的缓冲跟stdio的同步。如果你已经在头文件上用了using namespace std;那么就可以去掉前面的std::了。取消后就cin就不能和scanf,sscanf, getchar, fgets之类同时用了,否则就可能会导致输出和预期的不一样。

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    cout.sync_with_stdio(false);
    cout << "a
";
    printf("b
");
    cout << "c
";
}
输出结果是
b
a
c

取消同步的目的,是为了让cin不超时,另外cout的时候尽量少用endl,换用" ",也是防止超时的方法。

当然,尽量用scanf,printf就不用考虑这种因为缓冲的超时了。

原文地址:https://www.cnblogs.com/flipped/p/5543169.html