Namespaces
Variants

std::basic_stringstream<CharT,Traits,Allocator>:: basic_stringstream

From cppreference.net

(1)
(C++11 이전)
explicit basic_stringstream ( std:: ios_base :: openmode mode ) ;
(C++11 이후)
basic_stringstream ( )
: basic_stringstream ( std:: ios_base :: in | std:: ios_base :: out ) { }
(2) (C++11부터)
explicit basic_stringstream

( const std:: basic_string < CharT, Traits, Allocator > & str,
std:: ios_base :: openmode mode =

std:: ios_base :: in | std:: ios_base :: out ) ;
(3)
explicit basic_stringstream

( std:: basic_string < CharT, Traits, Allocator > && str,
std:: ios_base :: openmode mode =

std:: ios_base :: in | std:: ios_base :: out ) ;
(4) (C++20 이후)
basic_stringstream ( std:: ios_base :: openmode mode, const Allocator & a ) ;
(5) (C++20 이후)
template < class SAlloc >

basic_stringstream ( const std:: basic_string < CharT, Traits, SAlloc > & str,

std:: ios_base :: openmode mode, const Allocator & a ) ;
(6) (C++20 이후)
template < class SAlloc >

basic_stringstream ( const std:: basic_string < CharT, Traits, SAlloc > & str,
const Allocator & a )

: basic_stringstream ( str, std:: ios_base :: in | std:: ios_base :: out , a ) { }
(7) (C++20 이후)
template < class SAlloc >

explicit basic_stringstream
( const std:: basic_string < CharT, Traits, SAlloc > & str,
std:: ios_base :: openmode mode =

std:: ios_base :: in | std:: ios_base :: out ) ;
(8) (C++20 이후)
template < class StringViewLike >

explicit basic_stringstream
( const StringViewLike & t,
std:: ios_base :: openmode mode =

std:: ios_base :: in | std:: ios_base :: out ) ;
(9) (C++26부터)
template < class StringViewLike >

basic_stringstream ( const StringViewLike & t,

std:: ios_base :: openmode mode, const Allocator & a ) ;
(10) (C++26부터)
template < class StringViewLike >
basic_stringstream ( const StringViewLike & t, const Allocator & a ) ;
(11) (C++26부터)
basic_stringstream ( basic_stringstream && other ) ;
(12) (C++11 이후)

새로운 문자열 스트림을 생성합니다.

주어진

std::basic_iostream 베이스와 설명 전용 데이터 멤버 sb 는 다음과 같이 초기화됩니다.

오버로드
(Overload)
std::basic_iostream 베이스 sb
(1) base_type ( std:: addressof ( sb ) ) [1] buf_type ( mode )
(2) buf_type ( std:: ios_base :: in | std:: ios_base :: out )
(3) buf_type ( str, mode )
(4) buf_type ( std :: move ( str ) , mode )
(5) buf_type ( mode, a )
(6) buf_type ( str, mode, a )
(7) buf_type ( str, std:: ios_base :: in | std:: ios_base :: out , a )
(8) buf_type ( str, mode )
(9) std:: addressof ( sb ) { t, mode, Allocator ( ) }
(10) { t, mode, a }
(11) { t, std:: ios_base :: in | std:: ios_base :: out , a }
(12) other std::basic_iostream 베이스로부터 이동 생성됨 other.sb 로부터 이동 생성됨
  1. std::basic_iostream 기본 클래스는 C++11 이전까지 base_type ( & sb ) 로 초기화되었습니다 (오버로드 (1,3) 의 경우).
8) 이 오버로드는 std:: is_same_v < SAlloc, Allocator > false 인 경우에만 오버로드 해결에 참여합니다.
9-11) 이 오버로드들은 다음 조건이 std:: is_convertible_v < const StringViewLike & , std:: basic_string_view < CharT, Traits >> true 인 경우에만 오버로드 해결에 참여합니다.

목차

매개변수

str - 문자열 스트림의 초기 내용으로 사용할 문자열
t - 문자열 스트림의 초기 내용으로 사용할 객체 ( std::basic_string_view 로 변환 가능)
a - 문자열 스트림 내용 할당에 사용할 할당자
mode - 스트림 열기 모드를 지정합니다. BitmaskType 으로, 다음 상수들이 정의됩니다:
상수 설명
app 각 쓰기 작업 전에 스트림의 끝으로 이동
binary 바이너리 모드 로 열기
in 읽기용으로 열기
out 쓰기용으로 열기
trunc 열 때 스트림 내용 삭제
ate 열자마자 스트림의 끝으로 이동
noreplace (C++23) 배타적 모드로 열기
other - 소스로 사용할 다른 문자열 스트림

참고 사항

문자열 변환과 같이 빈번한 루프에서 일회성 basic_stringstream 객체를 생성하는 것은 동일한 객체를 재사용하기 위해 str() 함수를 호출하는 것보다 훨씬 더 많은 비용이 발생할 수 있습니다.

기능 테스트 매크로 표준 기능
__cpp_lib_sstream_from_string_view 202306L (C++26) std::stringstream std::string_view 연동, ( 9-11 )

예제

#include <iostream>
#include <sstream>
int main()
{
    // 기본 생성자 (입력/출력 스트림)
    std::stringstream buf1;
    buf1 << 7;
    int n = 0;
    buf1 >> n;
    std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n';
    // 입력 스트림
    std::istringstream inbuf("-10");
    inbuf >> n;
    std::cout << "n = " << n << '\n';
    // 추가 모드의 출력 스트림 (C++11)
    std::ostringstream buf2("test", std::ios_base::ate);
    buf2 << '1';
    std::cout << buf2.str() << '\n';
}

출력:

buf1 = 7 n = 7
n = -10
test1

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
P0935R0 C++11 기본 생성자가 explicit였음 implicit로 변경됨

참고 항목

기본 문자열 장치 객체의 내용을 가져오거나 설정합니다
(public member function)
basic_stringbuf 객체를 생성합니다
( std::basic_stringbuf<CharT,Traits,Allocator> 의 public member function)