名前空間
変種
操作

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

From cppreference.com
< cpp‎ | io‎ | basic stringbuf
 
 
 
 
(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以降)

基になる文字列を取得および設定します。

以下の説明では、buf および mode*this公開限定データメンバー です。

1) この std::basic_stringbuf の基になる文字シーケンスのコピーを含む std::basic_string オブジェクトを作成して返します。入力専用ストリームの場合、返される文字列は範囲 [eback()egptr()) の文字を含みます。入力/出力ストリームまたは出力専用ストリームの場合、egptr() および epptr() にかかわらず、pbase() からシーケンスの最後の文字までの文字を含みます。
書き込み用に開かれているバッファのメンバ文字シーケンスは、効率のためにオーバーアロケーションされる可能性があります。その場合、初期化された文字のみが返されます。これらの文字は、コンストラクタの文字列引数、str() のセッターオーバーロードの最新の呼び出しの文字列引数、または書き込み操作から取得された文字です。オーバーアロケーションを使用する典型的な実装では、バッファの初期化部分の末尾を追跡するためのハイウォーターマークポインタを維持し、このオーバーロードは pbase() からハイウォーターマークポインタまでの文字を返します。
return std::basic_string<CharT, Traits, Allocator>(view(), get_allocator()); と同等です。
(C++20以降)
2) (1) と同じですが、返される std::basic_string の構築に a が使用されます。return std::basic_string<CharT, Traits, SAlloc>(view(), a); と同等です。
このオーバーロードは、SAllocAllocator の要件を満たす場合にのみ、オーバーロード解決に参加します。
3) *this の基になる文字シーケンスを buf で移動構築するかのように std::basic_string オブジェクトを作成します。buf は、最初に (1) と同じ内容を含むように調整する必要がある場合があります。その後、buf を空に設定し、init_buf_ptrs() を呼び出して、std::basic_string オブジェクトを返します。
4) buf = s のように基になる文字シーケンスを置き換えてから、init_buf_ptrs() を呼び出します。
5) s のアロケータの型が Allocator ではない点を除いて (4) と同じです。
このオーバーロードは、std::is_same_v<SAlloc, Allocator>false の場合にのみ、オーバーロード解決に参加します。
6) buf = std::move(s) のように基になる文字シーケンスを置き換えてから、init_buf_ptrs() を呼び出します。
7) return std::basic_string_view<CharT, Traits> sv = t; のように t を文字列ビュー sv に暗黙的に変換してから、buf = sv のように基になる文字シーケンスを置き換え、init_buf_ptrs() を呼び出します。
このオーバーロードは、std::is_convertible_v<const StringViewLike&,
                      std::basic_string_view<CharT, Traits>>
true の場合にのみ、オーバーロード解決に参加します。

目次

[編集] パラメータ

s - 置き換え後の文字シーケンスを含む std::basic_string オブジェクト
t - 置き換え後の文字シーケンスを含むオブジェクト ( std::basic_string_view に変換可能)
a - 返される std::basic_string のすべてのメモリ割り当てに使用するアロケータ

[編集] 戻り値

1-3) このバッファの基になる文字シーケンスを含む std::basic_string オブジェクト。
4-7) (なし)

[編集] 注意

この関数は通常、std::basic_istringstream::str()std::basic_ostringstream::str()、または std::basic_stringstream::str() を介してアクセスされます。

機能テストマクロ 規格 機能
__cpp_lib_sstream_from_string_view 202306L (C++26) std::string_view と文字列ストリームの連携

[編集]

#include <iostream>
#include <sstream>
 
int main()
{
    int n;
 
    std::istringstream in;  // could also use in("1 2")
    in.rdbuf()->str("1 2"); // set the get area
    in >> n;
    std::cout << "after reading the first int from \"1 2\", the int is " 
              << n << ", str() = \"" << in.rdbuf()->str() << "\"\n"; // or in.str()
 
    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); // C++11
    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"

[編集] 欠陥レポート

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 432 C++98 1. オーバーロード (1) は、基になる文字シーケンスのコンテンツを指定していませんでした。
基になる文字シーケンスの
2. オーバーロード (4) は、入力および出力シーケンスの初期化方法を指定していませんでした。
入力および出力シーケンスの初期化方法
両方とも指定されました
LWG 562 C++98 オーバーロード (4) は、bool(mode & std::ios_base::out) == true の場合、基になる最後の文字の次の位置に epptr() を設定しました。
epptr() は、その位置を超えて設定される可能性があります。
epptr() は、その位置を超えて設定される可能性があります。
その位置を超えて設定される可能性があります。

[編集] 関連項目

基礎となる文字列デバイスオブジェクトの内容を取得または設定する
std::basic_stringstream<CharT,Traits,Allocator> の public メンバ関数) [編集]
(C++20)
基底文字シーケンスのビューを取得する
(public メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)