Namespaces
Variants

std::basic_stringbuf<CharT,Traits,Allocator>:: swap

From cppreference.net
void swap ( basic_stringbuf & rhs ) ;
(自 C++11 起)
(直至 C++20)
void swap ( basic_stringbuf & rhs ) noexcept ( /* 见下文 */ ) ;
(自 C++20 起)

交换 * this rhs 的状态及内容。

Allocator 不在交换时传播且 * this other 的分配器不相等,则行为未定义。

(since C++11)

目录

参数

rhs - 另一个 basic_stringbuf

返回值

(无)

异常

td> (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 << "交换前:one = " << std::quoted(one.str())
              << ", two = " << std::quoted(two.str()) << ".\n";
    one.rdbuf()->swap(*two.rdbuf());
    std::cout << "交换后:one = " << std::quoted(one.str())
              << ", two = " << std::quoted(two.str()) << ".\n";
}

输出:

交换前:one = "one", two = "two".
交换后:one = "two", two = "one".

参见

构造 basic_stringbuf 对象
(公开成员函数)
(C++11)
交换两个字符串流
( std::basic_stringstream<CharT,Traits,Allocator> 的公开成员函数)