写在前面

在无数的算法比赛中,不难看到下面这样的东西:

    ios::sync_with_stdio(false);

甚至是这样的东西:

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

现在,尽量用"\n"替换可以替换的endl 。(考虑流输出)

好了,以上就是我目前知道的技巧。

为什么

【ref】 sync_with_stdio(), tie()的应用

sync_with_stdio()

​ 这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑在了一起。

​ 在IO之前将stdio接触绑定,可以大大提高IO效率。在操作大数据时,cin,cout的速率也能很快了。

tie()

tie()用来绑定stream,空参数则返回当前的输出流指针。

【ref】Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);

【ref】C++中endl和\n的区别

  • 在考虑效率且没有必要刷新输出流时使用cout << . . . << "\n";
  • 在一些大程序需要刷新输出流时使用cout << . . . << endl;

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
  • 2021-11-14
  • 2021-10-26
  • 2022-12-23
  • 2021-08-10
  • 2021-12-13
猜你喜欢
  • 2021-06-27
  • 2021-09-04
  • 2021-12-03
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2021-03-30
相关资源
相似解决方案