名稱空間
變體
操作

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(/* 見下方 */);
(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 <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> 的公開成員函式) [編輯]