最終更新日時(UTC):
が更新

履歴 編集

class template
<sstream>

std::basic_ostringstream

namespace std {
  template <class CharT, class Traits = char_traits<CharT>,
            class Allocator = allocator<CharT> >
  class basic_ostringstream : public basic_ostream<CharT, Traits>;

  using ostringstream  = basic_ostringstream<char>;
  using wostringstream = basic_ostringstream<wchar_t>;
}

概要

std::basic_ostringstreamクラスは、文字列への書き込み操作ができる出力ストリームである。

このクラスは、内部バッファに文字列を保持し、ストリーム操作で文字列の内容を組み立てることができる。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ
(destructor) デストラクタ
operator= ムーブ代入 C++11
swap 値の交換 C++11
rdbuf ストリームバッファオブジェクトの設定・取得
str 文字列オブジェクトの設定・取得
view 文字列ビューオブジェクトの取得 C++20

非メンバ関数

名前 説明 対応バージョン
swap 2つのオブジェクトを入れ替える C++11

メンバ型

名前 説明 対応バージョン
char_type テンプレート仮引数CharT
int_type Traits::int_type
pos_type Traits::pos_type
off_type Traits::off_type
traits_type テンプレート仮引数Traits
allocator_type テンプレート仮引数Allocator

#include <iostream>
#include <sstream>
#include <string>

int main()
{
  std::ostringstream oss;

  // 数値や文字列を書き込む
  oss << "The answer is " << 42 << " and pi is approximately " << 3.14;

  // 文字列として取得
  std::string result = oss.str();
  std::cout << result << std::endl;

  // バッファをクリアして新しい内容を書き込む
  oss.str("");
  oss << "New content: " << 123.456;
  std::cout << oss.str() << std::endl;
}

出力

The answer is 42 and pi is approximately 3.14
New content: 123.456

参照