Namespaces
Variants

std::basic_stringstream<CharT,Traits,Allocator>:: basic_stringstream

From cppreference.net

(注:指定されたHTML要素には翻訳対象のテキストが含まれていないため、元の構造をそのまま保持しています)
(1)
(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,
std:: ios_base :: openmode mode =

std:: ios_base :: in | std:: ios_base :: out ) ;
(3)
explicit basic_stringstream

( std:: basic_string < CharT, Traits, Allocator > && str,
std:: ios_base :: openmode mode =

std:: ios_base :: in | std:: ios_base :: out ) ;
(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,

std:: ios_base :: openmode mode, const Allocator & a ) ;
(6) (C++20以降)
template < class SAlloc >

basic_stringstream ( const std:: basic_string < CharT, Traits, SAlloc > & str,
const Allocator & a )

: basic_stringstream ( str, std:: ios_base :: in | std:: ios_base :: out , a ) { }
(7) (C++20以降)
template < class SAlloc >

explicit basic_stringstream
( const std:: basic_string < CharT, Traits, SAlloc > & str,
std:: ios_base :: openmode mode =

std:: ios_base :: in | std:: ios_base :: out ) ;
(8) (C++20以降)
template < class StringViewLike >

explicit basic_stringstream
( const StringViewLike & t,
std:: ios_base :: openmode mode =

std:: ios_base :: in | std:: ios_base :: out ) ;
(9) (C++26以降)
template < class StringViewLike >

basic_stringstream ( const StringViewLike & t,

std:: ios_base :: openmode mode, const Allocator & a ) ;
(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以降)

新しい文字列ストリームを構築します。

与えられた

the 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 からムーブ構築
  1. std::basic_iostream 基底クラスはC++11まで base_type ( & sb ) で初期化されていました(オーバーロード (1,3) の場合)。
8) このオーバーロードは、 std:: is_same_v < SAlloc, Allocator > false の場合にのみ、オーバーロード解決に参加します。
9-11) これらのオーバーロードは、以下の条件が満たされる場合にのみオーバーロード解決に参加します: std:: is_convertible_v < const StringViewLike & , std:: basic_string_view < CharT, Traits >> true である場合です。

目次

パラメータ

str - 文字列ストリームの初期内容として使用する文字列
t - 文字列ストリームの初期内容として使用するオブジェクト( std::basic_string_view に変換可能)
a - 文字列ストリームの内容の割り当てに使用するアロケータ
mode - ストリームのオープンモードを指定。 BitmaskType であり、以下の定数が定義されている:
定数 説明
app 各書き込み前にストリームの終端へシーク
binary バイナリモード でオープン
in 読み取り用にオープン
out 書き込み用にオープン
trunc オープン時にストリームの内容を破棄
ate オープン直後にストリームの終端へシーク
noreplace (C++23) 排他モードでオープン
other - ソースとして使用する別の文字列ストリーム

注記

文字列変換などで使用される際のタイトなループ内での一回限りの basic_stringstream オブジェクトの構築は、 str() を呼び出して同じオブジェクトを再利用する場合よりも、著しくコストが高くなる可能性があります。

機能テスト マクロ 標準 機能
__cpp_lib_sstream_from_string_view 202306L (C++26) std::stringstream 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++規格に対して遡及的に適用されました。

DR Applied to Behavior as published Correct behavior
P0935R0 C++11 デフォルトコンストラクタは明示的でした 暗黙的になりました

関連項目

基となる文字列デバイスオブジェクトの内容を取得または設定する
(public member function)
basic_stringbuf オブジェクトを構築する
( std::basic_stringbuf<CharT,Traits,Allocator> のpublic member function)