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

来自cppreference.com
< cpp‎ | io‎ | basic stringbuf
void swap( std::basic_stringbuf& rhs )
(C++11 起)

交换 *thisrhs 的状态与内容。

参数

rhs - 另一 basic_stringbuf

返回值

(无)

注意

交换 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> 的公开成员函数)