它用於獲取/設置綁定流。
C++98
默認情況下,cin
綁定到cout
,wcin
綁定到wcout
。庫實現可以在初始化時綁定其他標準流。
C++11
默認情況下,標準窄流cin
和cerr
與cout
綁定,它們的寬字元對應(wcin
和wcerr
)綁定到wcout
。 庫實現也可以綁定clog
和wclog
。
聲明
以下是ios::tie
函數的聲明。
get (1) ostream* tie() const;
set (2) ostream* tie (ostream* tiestr);
第一種形式(1)返回指向綁定輸出流的指針。
第二種形式(2)將對象綁定到tiestr
,並返回一個指向調用之前綁定的流的指針(如果有的話)。
返回值
- 指向在調用之前綁定的流對象的指針,或者在流未綁定的情況下,則為空指針。
示例
在下面的例子中演示了ios::tie
函數的使用。
#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;
}
編譯和運行上面的程式,將產生以下結果 -
tie example:
This is inserted into cout
上一篇:
ios::imbue()函數
下一篇:
io::rdbuf()函數