Namespaces
Variants

std::ostrstream:: ostrstream

From cppreference.net
ostrstream ( ) ;
(1) (C++98で非推奨)
(C++26で削除)
ostrstream ( char * s, int n, std:: ios_base :: openmode mode = std:: ios_base :: out ) ;
(2) (C++98で非推奨)
(C++26で削除)

新しい出力 strstream とその基盤となる std::strstreambuf を構築します。

1) 基になる std::strstreambuf をデフォルト構築し、動的に拡張するバッファを作成し、基底クラスを strstreambuf メンバのアドレスで初期化します。
2) 基底クラスを、基盤となる std::strstreambuf メンバのアドレスで初期化しました。このメンバは、ユーザー提供の固定サイズ配列に書き込む2つの可能な方法のいずれかで初期化されます:
a) app ビットが mode に設定されていない場合、 strstreambuf ( s, n, s ) を呼び出してバッファを構築する。 s が指す配列の最初の要素から n 個未満の要素しか存在しない場合、動作は未定義である。
b) app ビットが mode に設定されている場合、 strstreambuf ( s, n, s + std:: strlen ( s ) ) を呼び出してバッファを構築する。 s が指す先頭要素を持つ配列に n 要素未満しか存在しない場合、または配列が有効なナル終端文字列を含まない場合、動作は未定義である。

パラメータ

s - 出力バッファとして使用するchar配列
n - 出力バッファとして使用する配列のサイズ
mode - ストリームのオープンモードを指定。ビットマスク型であり、以下の定数が定義されている(ただし app のみ使用される):
定数 説明
app 各書き込み前にストリームの終端へシーク
binary バイナリモードでオープン バイナリモード
in 読み取り用にオープン
out 書き込み用にオープン
trunc オープン時にストリームの内容を破棄
ate オープン直後にストリームの終端へシーク
noreplace (C++23) 排他モードでオープン

#include <iostream>
#include <strstream>
int main()
{
    std::ostrstream s1; // 動的バッファ
    s1 << 1 << ' ' << 3.14 << " example\n" << std::ends;
    std::cout << s1.str();
    s1.freeze(false);
    char arr[15] = "Hello";
    std::ostrstream s2(arr, sizeof arr, std::ios_base::app);
    s2 << ", world!" << std::ends;
    std::cout << s2.str() << '\n';
    std::cout << arr << '\n'; // ストリームは提供された配列を使用する
}

出力:

1 3.14 example
Hello, world!
Hello, world!

関連項目

strstreambuf オブジェクトを構築する
( std::strstreambuf のpublicメンバ関数)
istrstream オブジェクトを構築する(オプションでバッファを割り当てる)
( std::istrstream のpublicメンバ関数)
strstream オブジェクトを構築する(オプションでバッファを割り当てる)
( std::strstream のpublicメンバ関数)