CppDS.com

C++ 98 11 14 17 20 手册

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(/* see below */);
(C++20 起)

交换 *thisrhs 的状态与内容。

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

(C++20 起)

参数

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 <sstream>
#include <string>
#include <iostream>
 
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> 的公开成员函数)
关闭