public member function
<ios> <iostream>

std::ios::tie

get (1)
ostream* tie() const;
set (2)
ostream* tie (ostream* tiestr);
獲取/設定關聯流
第一種形式(1)返回指向已關聯的輸出流的指標。

第二種形式(2)將物件與 tiestr 關聯,並返回呼叫前已關聯流的指標(如果有)。

關聯流 是一個輸出流物件,它在當前流物件的每次 I/O 操作之前會被 *重新整理*。

預設情況下,cincout 關聯,wcinwcout 關聯。庫實現也可能在初始化時關聯其他標準流。
預設情況下,標準窄字元流 cincerrcout 關聯,它們的寬字元對應流(wcinwcerr)與 wcout 關聯。庫實現也可能將 clogwclog 關聯。

引數

tiestr
一個輸出流物件。

返回值

指向呼叫前已關聯的流物件的指標,如果流未關聯,則為 *空指標*。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// redefine tied object
#include <iostream>     // std::ostream, std::cout, std::cin
#include <fstream>      // std::ofstream

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


資料競爭

訪問(1)或修改(2)流物件。
併發訪問同一個流物件可能導致資料爭用。

異常安全

基本保證:如果丟擲異常,流處於有效狀態。