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\n";
    printf("b\n");
    cout << "c\n";
}
輸出結果是
b
a
c

 

取消同步的目的,是為了讓cin不超時,另外cout的時候盡量少用endl,換用"\n",也是防止超時的方法。

當然,盡量用scanf,printf就不用考慮這種因為緩沖的超時了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM