std::strstream::freeze
提供: cppreference.com
void freeze(bool flag = true); |
||
ストリームが出力用に動的確保された配列を使用している場合、バッファの自動確保/解放を無効化 (flag == true) または有効化 (flag == false) します。 実質的に rdbuf()->freeze(flag) を呼びます。
ノート
str() の呼び出しの後、動的ストリームは自動的に凍結状態になります。 この strstream オブジェクトが作成されたスコープを終了する前に freeze(false) の呼び出しが要求されます。 そうでなければ、デストラクタがメモリリークします。 また、凍結されたストリームへのさらなる出力は確保されたバッファの終端に達すると切り捨てられることがあります。
引数
| flag | - | 所望の状態 |
戻り値
(なし)
例
Run this code
#include <strstream>
#include <iostream>
int main()
{
std::strstream dyn; // dynamically-allocated output buffer
dyn << "Test: " << 1.23; // note: no std::ends to demonstrate appending
std::cout << "The output stream contains \"";
std::cout.write(dyn.str(), dyn.pcount()) << "\"\n";
// the stream is now frozen due to str()
dyn << " More text"; // output to a frozen stream may be truncated
std::cout << "The output stream contains \"";
std::cout.write(dyn.str(), dyn.pcount()) << "\"\n";
dyn.freeze(false); // freeze(false) must be called or the destructor will leak
std::strstream dyn2; // dynamically-allocated output buffer
dyn2 << "Test: " << 1.23; // note: no std::ends
std::cout << "The output stream contains \"";
std::cout.write(dyn2.str(), dyn2.pcount()) << "\"\n";
dyn2.freeze(false); // unfreeze the stream after str()
dyn2 << " More text" << std::ends; // output will not be truncated (buffer grows)
std::cout << "The output stream contains \"" << dyn2.str() << "\"\n";
dyn2.freeze(false); // freeze(false) must be called or the destructor will leak
}
出力例:
The output stream contains "Test: 1.23"
The output stream contains "Test: 1.23 More "
The output stream contains "Test: 1.23"
The output stream contains "Test: 1.23 More text"
関連項目
| バッファの凍結状態をセットまたはクリアします ( std::strstreambufのパブリックメンバ関数)
|