cin.tie(NULL)--加速

我是怎么在不知道这一对函数的情况下活到今天的,以前碰到cin TLE的时候总是傻乎乎地改成scanf,甚至还相信过C++在IO方面效率低下的鬼话,殊不知这只是C++为了兼容C而采取的保守措施。

tie

tie是将两个stream绑定的函数,空参数的话返回当前的输出流指针。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
 
///SubMain//
int  main( int  argc,  char  *argv[])
{
     std::ostream *prevstr;
     std::ofstream ofs;
     ofs.open( "test.txt" );
 
     std::cout <<  "tie example:\n" // 直接输出到屏幕
 
     *std::cin.tie() <<  "This is inserted into cout\n" ;   // 空参数调用返回默认的output stream,也就是cout
     prevstr = std::cin.tie(&ofs);                      // cin绑定ofs,返回原来的output stream
     *std::cin.tie() <<  "This is inserted into the file\n" // ofs,输出到文件
     std::cin.tie(prevstr);                                   // 恢复
 
     ofs.close();
     system ( "pause" );
     return  0;
}
///End Sub//

输出:

1
2
3
tie example:
This is inserted into cout
请按任意键继续. . .

同时当前目录下的test.txt输出:

1
This is inserted into the file

sync_with_stdio

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

应用

在ACM里,经常出现数据集超大造成 cin TLE的情况。这时候大部分人(包括原来我也是)认为这是cin的效率不及scanf的错,甚至还上升到C语言和C++语言的执行效率层面的无聊争论。其实像上文所说,这只是C++为了兼容而采取的保守措施。我们可以在IO之前将stdio解除绑定,这样做了之后要注意不要同时混用cout和printf之类。

在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。

如下所示:

1
2
3
4
5
6
7
#include <iostream>
int main() 
{
     std::ios::sync_with_stdio( false );
     std::cin.tie(0);
     // IO
}
来自http://www.bkjia.com/ASPjc/951313.html
  • 11
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值