std::ostrstream::str
From cppreference.com
< cpp | io | ostrstream
| char* str(); |
(C++98で非推奨) (C++26で削除) |
|
バッファをフリーズした後、バッファの先頭へのポインタを返します。実際には rdbuf()->str() を呼び出します。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
関連付けられた std::strstreambuf のバッファの先頭へのポインタ。バッファが利用できない場合はヌルポインタ。
注釈
str() を C 文字列として結果を使用する呼び出しの前に、ストリームバッファはヌル終端されている必要があります。通常、stream << 1.2 のような通常の出力はヌル終端文字を格納しないため、明示的に追加する必要があります。通常はマニピュレータ std::ends を使用します。
str() の呼び出し後、動的ストリームはフリーズされます。この ostrstream オブジェクトが作成されたスコープを抜ける前に freeze(false) を呼び出す必要があります。そうしないと、デストラクタがメモリリークを起こします。また、フリーズされたストリームへの追加の出力は、割り当てられたバッファの終わりに達すると切り捨てられる可能性があり、バッファがヌル終端されないままになる可能性があります。
[編集] 例
このコードを実行
#include <iostream> #include <strstream> int main() { std::ostrstream dyn; // dynamically-allocated output buffer dyn << "Test: " << 1.23; // not adding std::ends to demonstrate append behavior std::cout << "The output stream holds \""; std::cout.write(dyn.str(), dyn.pcount()) << "\"\n"; // the stream is now frozen due to str() dyn << " More text" << std::ends; std::cout << "The output stream holds \""; std::cout.write(dyn.str(), dyn.pcount()) << "\"\n"; dyn.freeze(false); }
実行結果の例
The stream holds "Test: 1.23" The stream holds "Test: 1.23 More "
関連項目
| バッファを凍結し、入力シーケンスの先頭ポインタを返します。 ( std::strstreambuf の public メンバー関数) |