fstream::swap()函數

它用於在x*this之間交換所有的內部數據。

聲明

下麵是fstream::swap的聲明。

C++11

void swap (basic_fstream& x);

參數

x −另一個相同類型的basic_ostream對象(即,具有相同的範本參數:charTtraits)。

示例

下麵的例子解釋了fstream::swap()函數用法。

#include <fstream>

int main () {
   std::fstream foo;
   std::fstream bar ("test.txt");

   foo.swap(bar);

   foo << "cpp fstream swap";

   foo.close();

   return 0;
}