一 作用
获取、设置绑定流
二 函数申明
get(1) | ostream* tie() const | 返回指向绑定输出流的指针 |
set(2) | ostream* tie(ostream* tiestr) | 将对象绑定到输出流tiestr,并返回之前绑定流的指针(如果有的话);若之前未绑定,返回空指针 |
三 注意
The tied stream is an output stream object which is flushed before each i/o operation in this stream object.
即:绑定的输出流在流对象的输入/输出操作前会刷新。
1 c++98
默认情况下,cin绑定到cout,wcin绑定到wcout。库实现可以在初始化时绑定其他标准流。
2 c++11
默认情况下,标准窄流cin和cerr与cout绑定,它们的宽字符对应(wcin和wcerr)绑定到wcout。 库实现也可以绑定clog和wclog。
四 举例
#include <iostream>
#include <fstream>
/*
* c++ ios::tie demo
*/
using std::cin;
int main()
{
std::ofstream ofs;
ofs.open("test.txt");
// 绑定流是一个输出流对象,它在这个流对象中的每个i/o操作之前刷新。
// 该条语句能做到实时刷新
cin.tie(&ofs);
char c;
while (cin >> c)
{
ofs << c;
}
ofs.close();
system("pause");
}
五 参考