std::basic_filebuf<CharT,Traits>::swap
來自 cppreference.com
< cpp | io | basic filebuf
void swap( std::basic_filebuf& rhs ); |
(C++11 起) | |
交換 *this 和 rhs 的狀態與內容。
目錄 |
[編輯] 引數
rhs | - | 另一個 basic_filebuf |
[編輯] 返回值
(無)
[編輯] 注意
此函式在交換 std::fstream 物件時自動呼叫,通常無需直接呼叫。
[編輯] 示例
執行此程式碼
#include <fstream> #include <iostream> #include <string> int main() { std::ifstream fin("test.in"); // read-only std::ofstream fout("test.out"); // write-only std::string s; getline(fin, s); std::cout << s << '\n'; // outputs the first line of test.in fin.rdbuf()->swap(*fout.rdbuf()); //swap the underlying buffers getline(fin, s); // fails: cannot read from a write-only filebuf std::cout << s << '\n'; // prints empty line }
[編輯] 參閱
(C++11) |
賦值一個 basic_filebuf 物件(public 成員函式) |
特化 std::swap 演算法 (函式模板) | |
(C++11) |
交換兩個檔案流 ( std::basic_fstream<CharT,Traits> 的 public 成員函式) |