一句话理解
cin.tie(nullptr) 解开 cin 和 cout 的绑定。
为什么要学
默认每次 cin 前都会刷新 cout,安全但慢。
讲解
绑定的目的是:先提示再输入时,提示能立刻出现。竞赛不需要提示,可以解开。
通常和 sync_with_stdio(false) 成对出现。
交互题才需要小心刷新。普通题解开即可。
例子
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
cout << n << "\n";
读和写都更快。最后用 \n 换行,一般不必 endl。
常见错误
- 交互题里解开绑定又不用 flush,对方可能收不到你的输出。
- 只 tie 不关同步,提升有限。