std::basic_ostringstream

std::basic_ostringstream (문자열 출력 스트림)

문자열 기반 스트림에 대한 출력 연산을 구현하는 클래스 템플릿이에요. std::basic_string 인스턴스를 실질적으로 저장하고 그에 대해 출력 연산을 수행해요.

출처: cppreference

본문

<sstream> 헤더에 정의돼 있어요.

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

클래스 템플릿 std::basic_ostringstream은 문자열 기반 스트림에 대한 출력 연산을 구현해요. std::basic_string 인스턴스를 실질적으로 저장하고 그에 대해 출력 연산을 수행해요. 저수준에서는 std::basic_stringbuf의 원시 문자열 장치 구현을 std::basic_ostream의 고수준 인터페이스로 감싸요. 고유 std::basic_stringbuf 멤버에 대한 완전한 인터페이스를 제공해요.

공통 문자 타입 typedef

  • std::ostringstreamstd::basic_ostringstream<char>
  • std::wostringstreamstd::basic_ostringstream<wchar_t>

멤버 함수

  • (constructor) — ostringstream을 생성해요(선택적으로 문자열로 초기화).
  • str — 기본 문자열을 얻거나 설정해요.
  • view (C++20) — 기본 문자열의 뷰를 얻어요.
  • rdbuf — 기반 stringbuf에 접근해요.
  • operator= — 이동 대입.

예제

#include <sstream>
#include <iostream>

int main()
{
    std::ostringstream oss;
    oss << "pi = " << 3.14159;
    std::cout << oss.str() << '\n'; // "pi = 3.14159"
}

더 알아보기 (Learn more)

cppreference