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

来自cppreference.com
< cpp‎ | io‎ | basic stringbuf
 
 
 
 
void swap( std::basic_stringbuf& rhs );
(C++11 起)
(C++20 前)
void swap( std::basic_stringbuf& rhs ) noexcept(/* 见下文 */);
(C++20 起)

交换 *thisrhs 的状态与内容。

如果 Allocator 不在交换时传播且 *thisother 的分配器不相等,那么行为未定义。

(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 <iostream>
#include <sstream>
#include <string>
 
int main()
{
    std::istringstream one("one");
    std::ostringstream two("two");
 
    std::cout << "Before swap, one = \"" << one.str() << '"'
              << " two = \"" << two.str() << "\"\n";
 
    *one.rdbuf()->swap(*two.rdbuf());
 
    std::cout << "Before swap, one = \"" << one.str() << '"'
              << " two = \"" << two.str() << "\"\n";
}

输出:

Before swap, one = "one" two = "two"
Before swap, one = "two" two = "one"

参阅

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