std::basic_ostream<CharT,Traits>:: swap
From cppreference.net
<
cpp
|
io
|
basic ostream
C++
Input/output library
| I/O manipulators | ||||
| Print functions (C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(C++20)
|
||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
| Synchronized Output | ||||
|
(C++20)
|
||||
| Types | ||||
| Error category interface | ||||
|
(C++11)
|
||||
|
(C++11)
|
std::basic_ostream
| Global objects | ||||
| Member functions | ||||
|
(C++11)
|
||||
| Formatted output | ||||
| Unformatted output | ||||
| Positioning | ||||
| Miscellaneous | ||||
|
basic_ostream::swap
(C++11)
|
||||
| Member classes | ||||
| Non-member functions | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
protected
:
void swap ( basic_ostream & rhs ) ; |
(自 C++11 起) | |
调用 basic_ios :: swap ( rhs ) 来交换基类中除 rdbuf ( ) 外所有数据成员,在 * this 与 rhs 之间进行交换。此交换函数为保护成员:它由可交换输出流类 std::basic_ofstream 和 std::basic_ostringstream 的交换函数调用,这些派生类知道如何正确交换关联的流缓冲区。
参数
| rhs | - | 用于交换的同类型 basic_ostream 对象 |
示例
运行此代码
#include <iostream> #include <sstream> #include <utility> int main() { std::ostringstream s1("hello"); std::ostringstream s2("bye"); s1.swap(s2); // 正确:ostringstream 具有公开的 swap() 方法 std::swap(s1, s2); // 正确:调用 s1.swap(s2) // std::cout.swap(s2); // 错误:swap 是受保护成员 std::cout << s1.str() << '\n'; }
输出:
hello