使用ios::sync_with_stdio(false);語句
cin,cout之所以效率低,是因爲先把要輸出的東西存入緩衝區,再輸出,導致效率降低,而這段語句可以來打消iostream的輸入輸出緩存,可以節省許多時間,使效率與scanf與printf相差無幾,但是還是比scanf與printf略慢,但已經不影響題目通過了。
sync_with_stdio
這個函數是一個“是否兼容stdio”的開關,C++爲了兼容C,保證程序在使用了std::printf和std::cout的時候不發生混亂,將輸出流綁到了一起。
#include<iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string a;
int x,y;
cin>>a>>x>>y;
cout<<a<<endl<<x<<endl<<y<<endl;
return 0;
}