std::strstream::str
From cppreference.com
| char* str(); |
(C++98で非推奨) (C++26で削除) |
|
バッファをフリーズした後、バッファの先頭へのポインタを返します。実際には rdbuf()->str() を呼び出します。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
関連付けられた std::strstreambuf のバッファの先頭へのポインタ。バッファが利用できない場合はヌルポインタ。
注釈
str()をC文字列として結果を使用する前に、ストリームバッファをヌル終端にする必要があります。通常の出力(例:stream << 1.2)はヌル終端を格納しないため、明示的に追加する必要があります。通常はマニピュレータ std::ends を使用します。
str()の呼び出し後、動的ストリームはフリーズします。この strstream オブジェクトが作成されたスコープを抜ける前に、freeze(false) を呼び出す必要があります。そうしないと、デストラクタでメモリリークが発生します。また、フリーズされたストリームへの追加出力は、割り当てられたバッファの終わりに達すると切り捨てられる可能性があり、バッファがヌル終端にならない場合があります。
[編集] 例
このコードを実行
#include <iostream> #include <strstream> int main() { std::strstream 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 メンバー関数) |