提升C++输入输出性能的三个方法

来源:互联网 发布:us域名注册 编辑:程序博客网 时间:2024/04/30 22:10

做题的时候,由于数据量大,很多情况下得用scanf和printf代替cin和cout用于输入输出。难道C++不行么?

百度了一下,有三条建议用于提高C++的输入输出速度:

  1. At the first line in main function,add :std::ios_base::sync_with_stdio(false).which cancel theSynchronization between <iostream> and <cstdio>;
  2. At the second line in main function,add: std::cin.tie(0).which leads to that cin ties nothing.cin ties cout at first.
  3. For all endl, use '\n' or"\n" instead.
例如:
<pre name="code" class="cpp">#include<iostream>#include<algorithm>#include<cstring>using namespace std;int main(){    std::ios::sync_with_stdio(false);    cin.tie(0);    int n,m;    cin>>n>>m;    cout<<n<<" "<<m<<"\n";    return 0;}


0 0