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

履歴 編集

function
<sstream>

std::basic_ostringstream::コンストラクタ

basic_ostringstream()
  : basic_ostringstream(ios_base::out) {}     // (1) C++11
explicit basic_ostringstream(
  ios_base::openmode which);                  // (2) C++11
explicit basic_ostringstream(
  ios_base::openmode which = ios_base::out);  // (1)+(2) C++03

explicit basic_ostringstream(
  const basic_string<CharT, Traits, Allocator>& s,
  ios_base::openmode which = ios_base::out);         // (2) C++03

explicit basic_ostringstream(
  basic_string<CharT, Traits, Allocator>&& s,
  ios_base::openmode which = ios_base::out);         // (3) C++20

basic_ostringstream(
  ios_base::openmode which,
  const Allocator& a);                               // (4) C++20

explicit basic_ostringstream(
  const basic_string<CharT, Traits, Allocator>& s,
  const Allocator& a)
    : basic_ostringstream(s, ios_base::out, a) {}    // (5) C++20

explicit basic_ostringstream(
  const basic_string<CharT, Traits, Allocator>& s,
  ios_base::openmode which,
  const Allocator& a);                               // (6) C++20

template<class SAlloc>
explicit basic_ostringstream(
  const basic_string<CharT, Traits, SAlloc>& s,
  ios_base::openmode which = ios_base::out);         // (7) C++20

template<class SAlloc>
basic_ostringstream(
  const basic_string<CharT, Traits, SAlloc>& s,
  const Allocator& a)
    : basic_ostringstream(s, ios_base::out, a) {}    // (8) C++20

template<class SAlloc>
basic_ostringstream(
  const basic_string<CharT, Traits, SAlloc>& s,
  ios_base::openmode which,
  const Allocator& a);                               // (9) C++20

basic_ostringstream(basic_ostringstream&& rhs);      // (10) C++11

template<class T>
explicit basic_ostringstream(
  const T& t,
  ios_base::openmode which = ios_base::out);         // (11) C++26

template<class T>
basic_ostringstream(
  const T& t,
  const Allocator& a);                               // (12) C++26

template<class T>
basic_ostringstream(
  const T& t,
  ios_base::openmode which,
  const Allocator& a);                               // (13) C++26

概要

basic_ostringstreamオブジェクトを構築する。

ここで、初期値として設定する文字列は、既存のファイルを上書きモードで開くことに似ており、ストリームの初期位置が先頭のまま、ストリーム内容の文字列を設定するものである。モードとしてstd::ios_base::ateを指定しすると、ストリームの初期位置が末尾に設定される。

  • (1) : デフォルトコンストラクタ
  • (2) : 指定されたモードで構築する
  • (3) : 初期文字列としてstd::basic_stringオブジェクトのコピーを指定して構築する
  • (4) : 指定されたモードとアロケータで構築する
  • (5) : 初期文字列としてstd::basic_stringオブジェクトのコピーと、アロケータを指定して構築する
  • (6) : 初期文字列としてstd::basic_stringオブジェクトのコピー、モード、アロケータを指定して構築する
  • (7) : 初期文字列としてAllocatorに変換可能なアロケータをもつstd::basic_stringオブジェクトのコピーと、モードを指定して構築する
  • (8) : 初期文字列としてAllocatorに変換可能なアロケータをもつstd::basic_stringオブジェクトのコピーと、アロケータを指定して構築する
  • (9) : 初期文字列としてAllocatorに変換可能なアロケータをもつstd::basic_stringオブジェクトのコピー、モード、アロケータを指定して構築する
  • (10) : ムーブコンストラクタ
  • (11) : 初期文字列としてstd::basic_string_viewに変換可能な文字列と、モードを指定して構築する
  • (12) : 初期文字列としてstd::basic_string_viewに変換可能な文字列と、アロケータを指定して構築する
  • (13) : 初期文字列としてstd::basic_string_viewに変換可能な文字列、モード、アロケータを指定して構築する

テンプレートパラメータ制約

  • (11), (12), (13) : is_convertible_v<const T&, basic_string_view<CharT, Traits>>trueであること

効果

  • (1) : 内部のbasic_stringbufオブジェクトをbasic_stringbuf<CharT, Traits, Allocator>(ios_base::out)で構築する
  • (2) : 内部のbasic_stringbufオブジェクトをbasic_stringbuf<CharT, Traits, Allocator>(which | ios_base::out)で構築する
  • (3) : 内部のbasic_stringbufオブジェクトをbasic_stringbuf<CharT, Traits, Allocator>(s, which | ios_base::out)で構築する
  • (4) : 内部のbasic_stringbufオブジェクトをbasic_stringbuf<CharT, Traits, Allocator>(std::move(s), which | ios_base::out)で構築する
  • (5) : 内部のbasic_stringbufオブジェクトをbasic_stringbuf<CharT, Traits, Allocator>(which | ios_base::out, a)で構築する
  • (6), (7), (8), (9) : 各引数が対応するbasic_stringbufのコンストラクタに渡される
  • (10) : rhsからbasic_ostringstreamオブジェクトをムーブ構築する
  • (11) : basic_string_view<CharT, Traits>(t)で文字列を初期化し、モードはwhich | ios_base::outに設定する
  • (12) : basic_string_view<CharT, Traits>(t)で文字列を初期化し、モードはios_base::out、アロケータはaに設定する
  • (13) : basic_string_view<CharT, Traits>(t)で文字列を初期化し、モードはwhich | ios_base::out、アロケータはaに設定する

基本的な使い方

#include <iostream>
#include <sstream>

int main()
{
  // (1) デフォルト構築
  std::ostringstream ss1;
  ss1 << "default";
  std::cout << ss1.str() << std::endl;

  // (3) 文字列を指定して構築
  std::ostringstream ss2("xxxx yyyy");
  ss2 << "text";
  std::cout << ss2.str() << std::endl;
}

出力

default
text yyyy

string_viewからの構築 (C++26)

#include <iostream>
#include <sstream>
#include <string_view>

int main()
{
  // 文字列リテラルから構築
  std::ostringstream ss1{"Hello?", std::ios_base::ate};
  ss1 << "World";
  std::cout << ss1.str() << std::endl;

  // string_viewから構築
  std::string_view sv = "Hello?";
  std::ostringstream ss2{sv};
  ss2 << "World";
  std::cout << ss2.str() << std::endl;
}

出力

Hello?World
World?

参照