Namespaces
Variants

std::basic_istringstream<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 >> true の場合にのみ、オーバーロード解決に参加します。

目次

パラメータ

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"

関連項目

基となる生文字列デバイスオブジェクトを返す
(公開メンバ関数)
関連付けられた文字列のコピーを取得または置換する
( std::basic_stringbuf<CharT,Traits,Allocator> の公開メンバ関数)