std::basic_stringstream<CharT,Traits,Allocator>::basic_stringstream
(1) | ||
explicit basic_stringstream( std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out ); |
(C++11 前) | |
explicit basic_stringstream( std::ios_base::openmode mode ); |
(C++11 起) | |
basic_stringstream() : basic_stringstream(std::ios_base::in | std::ios_base::out) {} |
(2) | (C++11 起) |
explicit basic_stringstream ( const std::basic_string<CharT, Traits, Allocator>& str, |
(3) | |
explicit basic_stringstream ( std::basic_string<CharT, Traits, Allocator>&& str, |
(4) | (C++20 起) |
basic_stringstream( std::ios_base::openmode mode, const Allocator& a ); |
(5) | (C++20 起) |
template< class SAlloc > basic_stringstream( const std::basic_string<CharT, Traits, SAlloc>& str, |
(6) | (C++20 起) |
template< class SAlloc > basic_stringstream( const std::basic_string<CharT, Traits, SAlloc>& str, |
(7) | (C++20 起) |
template< class SAlloc > explicit basic_stringstream |
(8) | (C++20 起) |
template< class StringViewLike > explicit basic_stringstream |
(9) | (C++26 起) |
template< class StringViewLike > basic_stringstream( const StringViewLike& t, |
(10) | (C++26 起) |
template< class StringViewLike > basic_stringstream( const StringViewLike& t, const Allocator& a ); |
(11) | (C++26 起) |
basic_stringstream( basic_stringstream&& other ); |
(12) | (C++11 起) |
構造新的字符串流。
給定
-
base_type
為 std::basic_iostream<CharT, Traits>, -
buf_type
為 std::basic_stringbuf<CharT, Traits, Allocator>,
按以下方式初始化基類 std::basic_iostream 和僅用於闡述的數據成員 sb
。
重載 | std::basic_iostream 基類 | sb
|
---|---|---|
(1) | base_type(std::addressof(sb))[1] | buf_type(mode) |
(2) | buf_type(std::ios_base::in | std::ios_base::out) | |
(3) | buf_type(str, mode) | |
(4) | buf_type(std::move(str), mode) | |
(5) | buf_type(mode, a) | |
(6) | buf_type(str, mode, a) | |
(7) | buf_type(str, std::ios_base::in | std::ios_base::out, a) | |
(8) | buf_type(str, mode) | |
(9) | std::addressof(sb) | {t, mode, Allocator()} |
(10) | {t, mode, a} | |
(11) | {t, std::ios_base::in | std::ios_base::out, a} | |
(12) | 從 other 的 std::basic_iostream 基類 移動構造 |
從 other.sb 移動構造 |
- ↑ 在 C++11 前基類 std::basic_iostream 會以 base_type(&sb) 初始化(對於重載 (1,3))。
目錄 |
[編輯] 參數
str | - | 用作字符串流的初始內容的字符串 | ||||||||||||||||
t | - | 用作字符串流的初始內容的對象(可轉換到 std::basic_string_view) | ||||||||||||||||
a | - | 用於分配字符串流的內容的分配器 | ||||||||||||||||
mode | - | 指定流打開模式。它是一種位掩碼類型 (BitmaskType) ,定義了下列常量:
| ||||||||||||||||
other | - | 用作源的另一字符串流 |
[編輯] 註解
在短的循環中,例如用於字符串轉換時,構造單次使用的 basic_stringstream
對象,開銷可能顯著高於調用 str() 並復用同一對象。
功能特性測試宏 | 值 | 標準 | 功能特性 |
---|---|---|---|
__cpp_lib_sstream_from_string_view |
202306L |
(C++26) | 字符串流的 std::string_view 接口,(9-11) |
[編輯] 示例
#include <iostream> #include <sstream> int main() { // 默认构造函数(输入/输出流) std::stringstream buf1; buf1 << 7; int n = 0; buf1 >> n; std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n'; // 输入流 std::istringstream inbuf("-10"); inbuf >> n; std::cout << "n = " << n << '\n'; // 追加模式的输出流(C++11) std::ostringstream buf2("test", std::ios_base::ate); buf2 << '1'; std::cout << buf2.str() << '\n'; }
輸出:
buf1 = 7 n = 7 n = -10 test1
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 出版時的行為 | 正確行為 |
---|---|---|---|
P0935R0 | C++11 | 默認構造函數是顯式的 | 改成隱式的 |
[編輯] 參閱
獲取或設置底層字符串設備對象的內容 (公開成員函數) | |
構造一個 basic_stringbuf 對象 ( std::basic_stringbuf<CharT,Traits,Allocator> 的公開成員函數)
|