C++的tie()函数
来源:互联网 发布:知乎绝地求生炸鱼 编辑:程序博客网 时间:2024/06/06 01:29
链接:http://tech.quarkjoker.com/2011/06/22/C++%E7%9A%84tie()%E5%87%BD%E6%95%B0
今天看C ++ primer 时,看到了ios::tie() 函数。不明白其含义,跟不知道作用。于是上网查了一下。
cplusplus.com 给出的定义是:
ostream* tie ( ) const; //返回指向绑定的输出流的指针。
ostream* tie ( ostream* tiestr ); //将tiestr指向的输出流绑定的该对象上,并返回上一个绑定的输出流指针。
什么意思呢?
就是说对于一个ios(输入输出流)对象,可以把一个输出流和它“绑定”起来。不带参数执行,返回“绑定”的输出流指针;带一个输出流指针作为参数,则重新设置绑定的对象,并返回前任绑定对象(指针)。
但是,什么是“绑定”呢?
比方说,对于下面这个程序:
#include <iostream>#include <fstream>using namespace std;int main () { ofstream ofs; ofs.open ("test.txt"); cin.tie (&ofs); //注释掉这一行试试看 *cin.tie() << "There will be some text:"; char c; while(cin >> c){ ofs << c; } ofs.close(); return 0;}
用linux下watch命令(windows下还不知道有没有什么类似的东西,如果没有就用本办法:手动关掉再打开)实时察看test.txt文件,会发现每当你在终端里敲几个字后按下回车,test.txt文件里的文字就多了几个。
而如果你将代码中标注的那一行注释掉,就会发现,test.txt只有在程序运行结束(linux下按ctrl+d,windows下是ctrl+z结束输入)后才会有东西出现。
这就是“绑定”的效果,每当被“绑定”的对象有出入或输出操作,就会自动刷新“绑定”的对象的缓冲区,以达到实时的效果。
Remarks
tie causes two streams to be synchronized, such that, operations on one stream occur after operations on the other stream are complete.
Example
In this example, by tying cin to cout, it is guaranteed that the "Enter a number:" string will go to the console before the number itself is extracted from cin. This eliminates the possibility that the "Enter a number:" string is still sitting in the buffer when the number is read, so that we are certain that the user actually has some prompt to respond to. By default, cin and cout are tied.
#include <ios>#include <iostream>int main( ) { using namespace std; int i; cin.tie( &cout ); cout << "Enter a number:"; cin >> i;}
- C++的tie()函数
- C++的tie()函数
- C++的tie()函数
- C++的tie()函数
- tie的作用
- Tie
- tie
- iostream/fstream中的输入输出流指针的绑定,tie函数的使用。
- TIE绘制图形的文件保存实现
- Silverlight 中一些必须知道的技巧(zhuan tie)
- c++ tr1和Boost中,tuple、tie的用法
- 基于二进制程序的类型推导--TIE: Principled Reverse Engineering of Types in Binary Programs
- 联合 tie-up
- Grid-tie inverter
- 【Perl】模块 Tie::File
- cin.tie(NULL)--加速
- Tuple and Tie
- stl中的tuple(tie)
- 开始学习PYTHON
- 获取当前经纬度方法
- windows 编程- HelloMsg
- MATLAB中PLOT 的例子 源代码
- 编程之美-前言
- C++的tie()函数
- Turn on WCF Server Security Event Logging & Auditing
- 查看SQL2008列说明
- MicroKernel & Exokernel 操作系统未来可能的发展
- SHSH备份教程及相关知识介绍
- 使用C#开发基于Winform的手机号码归属地查询工具
- daodejing
- ofbiz 网站或店铺视觉主题(visual Theme)设计
- CSDN论坛上的一些面试题收集