std::basic_stringbuf<CharT,Traits,Allocator>::swap
来自 cppreference.com
< cpp | io | basic stringbuf
void swap( basic_stringbuf& rhs ); |
(自 C++11 起) (直至 C++20) |
|
void swap( basic_stringbuf& rhs ) noexcept(/* see below */); |
(自 C++20 起) | |
交换 *this 和 rhs 的状态和内容。
如果 |
(自 C++11 起) |
内容 |
[编辑] 参数
rhs | - | 另一个 basic_stringbuf |
[编辑] 返回值
(无)
[编辑] 异常
可能抛出实现定义的异常。 |
(自 C++11 起) (直至 C++20) |
noexcept 规范:
noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value || std::allocator_traits<Allocator>::is_always_equal::value) |
(自 C++20 起) |
[编辑] 备注
当交换 std::stringstream 对象时,此函数会自动调用。很少需要直接调用它。
[编辑] 示例
运行此代码
#include <iomanip> #include <iostream> #include <sstream> #include <string> int main() { std::istringstream one("one"); std::ostringstream two("two"); std::cout << "Before swap: one = " << std::quoted(one.str()) << ", two = " << std::quoted(two.str()) << ".\n"; one.rdbuf()->swap(*two.rdbuf()); std::cout << "After swap: one = " << std::quoted(one.str()) << ", two = " << std::quoted(two.str()) << ".\n"; }
输出
Before swap: one = "one", two = "two". After swap: one = "two", two = "one".
[编辑] 另请参阅
构造一个 basic_stringbuf 对象(公有成员函数) | |
(C++11) |
交换两个字符串流 ( std::basic_stringstream<CharT,Traits,Allocator> 的公有成员函数) |