Namespaces
Variants

std::strstream:: strstream

From cppreference.net
strstream ( ) ;
(1) (C++98で非推奨)
(C++26で削除)
(2) (C++98で非推奨)
(C++26で削除)

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

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

目次

パラメータ

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

#include <iostream>
#include <string>
#include <strstream>
int main()
{
    // 動的バッファ
    std::strstream s1;
    s1 << 1 << ' ' << 3.14 << " example" << std::ends;
    std::cout << "Buffer holds: '" << s1.str() << "'\n";
    s1.freeze(false);
    int n;
    double d;
    std::string w;
    s1 >> n >> d >> w;
    std::cout << "Read back: n = " << n
              << ", d = " << d
              << ", w = '" << w << "'\n";
    // 静的バッファ
    char arr[20] = "-1 -3.14 ";
    std::strstream s2(arr, sizeof arr, std::ios_base::app);
    s2 << "another" << std::ends;
    std::cout << "Buffer holds: '" << s2.str() << "'\n";
    s2 >> n >> d >> w;
    std::cout << "Read back: n = " << n
              << ", d = " << d
              << ", w = '" << w << "'\n";
}

出力:

Buffer holds: '1 3.14 example'
Read back: n = 1, d = 3.14, w = 'example'
Buffer holds: '-1 -3.14 another'
Read back: n = -1, d = -3.14, w = 'another'

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 115 C++98 オーバーロード (2) は
mode & app == 0 の場合のみ考慮していた
( == & より優先順位が高い)
( mode & app ) == 0 の場合と
( mode & app ) ! = 0 の場合の両方を考慮する

関連項目

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