今天刷到剑指offer的第35题 看到大佬的程序有以下神仙操作
本渣渣研究了一下~果然是块好砖,哪儿哪儿都能搬!
static const auto io_sync_off = []() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return nullptr;
}( );
代码讲解
1.static const auto io_sync_off = {}
这玩意是Lambda表达式(匿名函数)
2.sync_with_stdio(false)详解 链接: link.
具体写法可以是:std::ios::sync_with_stdio(false);
或者: ios_base::sync_with_stdio(false);
事实上ios是父类,ios_base是ios的子类,所以ios可以访问到ios_base的所有成员。
作用:让std :: cin和std :: cout不再经过缓冲区,iostream的性能就会提高了很多倍。
3.std::cin.tie(nullptr)
tie(nullptr)来解除std :: cin和std :: cout之间的绑定,进一步加快执行效率。
更详细的代码讲解链接: link.