它用于獲取/設(shè)置綁定流。
默認(rèn)情況下,cin綁定到cout,wcin綁定到wcout。庫實(shí)現(xiàn)可以在初始化時綁定其他標(biāo)準(zhǔn)流。
默認(rèn)情況下,標(biāo)準(zhǔn)窄流cin和cerr與cout綁定,它們的寬字符對應(yīng)(wcin和wcerr)綁定到wcout。 庫實(shí)現(xiàn)也可以綁定clog和wclog。
以下是ios::tie函數(shù)的聲明。
get (1) ostream* tie() const;
set (2) ostream* tie (ostream* tiestr);
第一種形式(1)返回指向綁定輸出流的指針。
第二種形式(2)將對象綁定到tiestr,并返回一個指向調(diào)用之前綁定的流的指針(如果有的話)。
在下面的例子中演示了ios::tie函數(shù)的使用。
#include <iostream>
#include <fstream>
int main () {
std::ostream *prevstr;
std::ofstream ofs;
ofs.open ("test.txt");
std::cout << "tie example:/n";
*std::cin.tie() << "This is inserted into cout";
prevstr = std::cin.tie (&ofs);
*std::cin.tie() << "This is inserted into the file";
std::cin.tie (prevstr);
ofs.close();
return 0;
}
編譯和運(yùn)行上面的程序,將產(chǎn)生以下結(jié)果 -
tie example:
This is inserted into cout