命名空间
变体
操作

std::strstream::freeze

来自 cppreference.cn
< cpp‎ | io‎ | strstream
void freeze( bool flag = true );
(C++98 中已弃用)
(C++26 中已移除)

如果流正在使用动态分配的数组进行输出,则禁用 (flag == true) 或启用 (flag == false) 缓冲区的自动分配/释放。 有效地调用 rdbuf()->freeze(flag)

目录

[edit] 注解

在调用 str() 之后,动态流将自动冻结。 在退出创建此 strstream 对象的作用域之前,需要调用 freeze(false),否则析构函数将泄漏内存。 此外,一旦超出已分配缓冲区末尾,对冻结流的额外输出可能会被截断。

[edit] 参数

flag - 期望状态

[edit] 返回值

(无)

[edit] 示例

#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"

[edit] 参见

设置/清除缓冲区的冻结状态
std::strstreambuf 的公共成员函数) [编辑]