std::basic_stringbuf<CharT,Traits,Allocator>:: str
From cppreference.net
<
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 は exposition-only data members であり、 * this のメンバです。
1)
この
std::basic_stringbuf
の基となる文字シーケンスのコピーを含む
std::basic_string
オブジェクトを作成して返す。入力専用ストリームの場合、返される文字列は範囲
[
eback()
,
egptr()
)
の文字を含む。入出力または出力専用ストリームの場合、
pbase()
からシーケンスの最後の文字までの文字を含み、これは
egptr()
および
epptr()
に関係しない。
-
-
書き込み用に開かれたバッファ内のメンバー文字シーケンスは、効率化のためにオーバーアロケーションされる場合があります。その場合、
初期化済み文字
のみが返されます:これらの文字は、コンストラクタの文字列引数、
str()のセッターオーバーロードの最新の呼び出しにおける文字列引数、または書き込み操作から取得されたものです。オーバーアロケーションを使用する典型的な実装では、バッファの初期化済み部分の終端を追跡するためのハイウォーターマークポインタが維持され、このオーバーロードは pbase() からハイウォーターマークポインタまでの文字を返します。
-
書き込み用に開かれたバッファ内のメンバー文字シーケンスは、効率化のためにオーバーアロケーションされる場合があります。その場合、
初期化済み文字
のみが返されます:これらの文字は、コンストラクタの文字列引数、
|
(C++20以降) |
2)
(1)
と同様であるが、
a
が返される
std::basic_string
の構築に使用される点が異なる。
return
std::
basic_string
<
CharT, Traits, SAlloc
>
(
view
(
)
, a
)
;
と等価。
3)
std::basic_string
オブジェクトを、
*
this
の
buf
内の基底文字シーケンスからムーブ構築するかのように作成する。
buf
は最初に
(1)
と同じ内容を含むように調整が必要な場合がある。その後、
buf
を空に設定し、
init_buf_ptrs
()
を呼び出し、その後
std::basic_string
オブジェクトを返す。
5)
(4)
と同様だが、
s
のアロケータの型が
Allocator
ではない点が異なる。
7)
t
を文字列ビュー
sv
に、あたかも
std::
basic_string_view
<
CharT, Traits
>
sv
=
t
;
によって暗黙的に変換し、その後、基礎となる文字シーケンスを
buf
=
sv
によって置き換え、その後
init_buf_ptrs
()
を呼び出します。
このオーバーロードは、以下の条件が満たされる場合にのみオーバーロード解決に参加します:
std::
is_convertible_v
<
const
StringViewLike
&
,
std:: basic_string_view < CharT, Traits >> が true である場合。
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)
は
epptr()
を最後の基底文字の
次の位置を指すように設定していた( bool ( mode & std:: ios_base :: out ) == true の場合) |
epptr()
はその位置を
超えて設定可能 |
関連項目
|
基盤となる文字列デバイスオブジェクトの内容を取得または設定する
(
std::basic_stringstream<CharT,Traits,Allocator>
の公開メンバ関数)
|
|
|
(C++20)
|
基盤となる文字シーケンスに対するビューを取得する
(公開メンバ関数) |