c++ 刷题还有这种高效操作?sync_with_stdio(false)~cin.tie(nullptr)

今天刷到剑指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.

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值