名前空間
変種
操作

std::strstream::freeze

From cppreference.com
< cpp‎ | io‎ | strstream
 
 
 
 
void freeze( bool flag = true );
(C++98で非推奨)
(C++26で削除)

動的に割り当てられた配列を出力に使用しているストリームの場合、バッファの自動割り当て/解除を無効にします(flag == true)。または有効にします(flag == false)。実質的には rdbuf()->freeze(flag) を呼び出します。

目次

[編集] 注釈

str() を呼び出した後、動的ストリームは自動的にフリーズされます。この strstream オブジェクトが作成されたスコープを抜ける前に freeze(false) を呼び出す必要があります。そうしないと、デストラクタでメモリリークが発生します。また、フリーズされたストリームへの追加の出力は、割り当てられたバッファの終わりに達すると切り捨てられる可能性があります。

[編集] パラメータ

flag - 目的の状態

[編集] 戻り値

(なし)

[編集]

#include <iostream>
#include <strstream>
 
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 の public メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)