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 起) | |
交換 *this 和 rhs 的狀態和內容。
若 |
(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> 的公開成員函式) |