Namespaces
Variants

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

From cppreference.net

(1)
std:: basic_string < CharT, Traits, Allocator > str ( ) const ;
(C++20 이전)
std:: basic_string < CharT, Traits, Allocator > str ( ) const & ;
(C++20 이후)
template < class SAlloc >
std:: basic_string < CharT, Traits, SAlloc > str ( const SAlloc & a ) const ;
(2) (C++20 이후)
std:: basic_string < CharT, Traits, Allocator > str ( ) && ;
(3) (C++20 이후)
void str ( const std:: basic_string < CharT, Traits, Allocator > & s ) ;
(4)
template < class SAlloc >
void str ( const std:: basic_string < CharT, Traits, SAlloc > & s ) ;
(5) (C++20 이후)
void str ( std:: basic_string < CharT, Traits, Allocator > && s ) ;
(6) (C++20 이후)
template < class StringViewLike >
void str ( const StringViewLike & t ) ;
(7) (C++26 이후)

기본 문자열 객체의 내용을 관리합니다.

1) 기본 문자열의 복사본을 반환합니다. 다음 코드와 동일합니다: return rdbuf ( ) - > str ( ) ; .
2) 기본 문자열의 복사본을 반환하며, a 를 할당자로 사용합니다. 다음 코드와 동일합니다: return rdbuf ( ) - > str ( a ) ; .
3) 기본 문자열에서 이동 생성된 문자열을 반환합니다. 다음과 동일합니다: return std :: move ( * rdbuf ( ) ) . str ( ) ; .
4,5) 기본 문자열의 내용을 대체합니다. 다음 코드와 동일합니다: rdbuf ( ) - > str ( s ) ; .
6) 기본 문자열의 내용을 대체합니다. 다음 코드와 동일합니다: rdbuf ( ) - > str ( std :: move ( s ) ) ; .
7) 기본 문자열의 내용을 대체합니다. 다음 코드와 동일합니다: rdbuf ( ) - > str ( t ) ; .
이 오버로드는 다음 조건이 is_convertible_v < const T & , basic_string_view < charT, traits >> 일 때만 오버로드 해결에 참여합니다.

목차

매개변수

s - 기본 문자열의 새로운 내용
t - 기본 문자열의 새로운 내용으로 사용할 객체 ( std::basic_string_view 로 변환 가능)
a - 반환된 문자열을 구성하는 데 사용할 할당자

반환값

1,2) 기본 문자열 객체의 복사본.
3) 기본 문자열 객체에서 이동 생성된 문자열입니다.
4-7) (없음)

참고 사항

str 에 의해 반환된 기반 문자열의 복사본은 표현식의 끝에서 소멸될 임시 객체이므로, str ( ) 의 결과에 직접 c_str() 를 호출하는 경우(예: auto * ptr = out. str ( ) . c_str ( ) ; ) 댕글링 포인터가 생성됩니다.

기능 테스트 매크로 표준 기능
__cpp_lib_sstream_from_string_view 202306L (C++26) std::stringstream std::string_view 간의 인터페이싱, ( 7 )

예제

#include <iostream>
#include <sstream>
int main()
{
    int n;
    std::istringstream in; // could also use in("1 2")
    in.str("1 2");
    in >> n;
    std::cout << "After reading the first int from \"1 2\", the int is "
              << n << ", str() = \"" << in.str() << "\"\n";
    std::ostringstream out("1 2");
    out << 3;
    std::cout << "After writing the int '3' to output stream \"1 2\""
              << ", str() = \"" << out.str() << "\"\n";
    std::ostringstream ate("1 2", std::ios_base::ate);
    ate << 3;
    std::cout << "After writing the int '3' to append stream \"1 2\""
              << ", str() = \"" << ate.str() << "\"\n";
}

출력:

After reading the first int from "1 2", the int is 1, str() = "1 2"
After writing the int '3' to output stream "1 2", str() = "3 2"
After writing the int '3' to append stream "1 2", str() = "1 23"

참고 항목

기본 원시 문자열 장치 객체를 반환합니다
(public member function)
연결된 문자열의 복사본을 교체하거나 획득합니다
( std::basic_stringbuf<CharT,Traits,Allocator> 의 public member function)