Namespaces
Variants

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

From cppreference.net

HTMLタグ、属性、C++コード内のテキストは翻訳せず、元のフォーマットを保持しています。 (注:このHTML要素には翻訳対象のテキストコンテンツが含まれていないため、元の構造を保持したまま出力します)
(1)
explicit basic_ostringstream ( std:: ios_base :: openmode mode =
std:: ios_base :: out ) ;
(C++11まで)
explicit basic_ostringstream ( std:: ios_base :: openmode mode ) ;
(C++11以降)
basic_ostringstream ( )
: basic_ostringstream ( std:: ios_base :: out ) { }
(2) (C++11以降)
explicit basic_ostringstream

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

std:: ios_base :: out ) ;
(3)
explicit basic_ostringstream

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

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

basic_ostringstream ( const std:: basic_string < CharT, Traits, SAlloc > & str,

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

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

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

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

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

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

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

basic_ostringstream ( const StringViewLike & t,

std:: ios_base :: openmode mode, const Allocator & a ) ;
(10) (C++26以降)
template < class StringViewLike >
basic_ostringstream ( const StringViewLike & t, const Allocator & a ) ;
(11) (C++26以降)
basic_ostringstream ( basic_ostringstream && other ) ;
(12) (C++11以降)

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

与えられた

std::basic_ostream 基底クラスと 説明専用データメンバ sb は以下のように初期化されます。

オーバーロード
番号
std::basic_ostream 基底クラス sb
(1) base_type ( std:: addressof ( sb ) ) [1] buf_type ( mode | std:: ios_base :: out )
(2) buf_type ( std:: ios_base :: out )
(3) buf_type ( str, mode | std:: ios_base :: out )
(4) buf_type ( std :: move ( str ) , mode | std:: ios_base :: out )
(5) buf_type ( mode | std:: ios_base :: out , a )
(6) buf_type ( str, mode | std:: ios_base :: out , a )
(7) buf_type ( str, std:: ios_base :: out , a )
(8) buf_type ( str, mode | std:: ios_base :: out )
(9) std:: addressof ( sb ) { t, mode | std:: ios_base :: out , Allocator ( ) }
(10) { t, mode | std:: ios_base :: out , a }
(11) { t, std:: ios_base :: out , a }
(12) other std::basic_ostream 基底クラスからムーブ構築 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_ostringstream オブジェクトの構築を、文字列変換に使用される場合などのタイトループ内で行うことは、 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 デフォルトコンストラクタは明示的でした 暗黙的になりました

関連項目

基になる文字列デバイスオブジェクトの内容を取得または設定する
(公開メンバ関数)
basic_stringbuf オブジェクトを構築する
( std::basic_stringbuf<CharT,Traits,Allocator> の公開メンバ関数)