std::ios::sync_with_stdio(false);

這句語句是用來取消cin的同步,什麼叫同步呢?就是iostream的緩衝跟stdio的同步。若是你已經在頭文件上用了using namespace std;那麼就能夠去掉前面的std::了。取消後就cin就不能和scanf,sscanf, getchar, fgets之類同時用了,不然就可能會致使輸出和預期的不同。ios

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

 

取消同步的目的,是爲了讓cin不超時,另外cout的時候儘可能少用endl,換用"\n",也是防止超時的方法。spa

固然,儘可能用scanf,printf就不用考慮這種由於緩衝的超時了。code

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息