std::ostrstream::freeze
来自 cppreference.com
< cpp | io | ostrstream
void freeze( bool flag = true ); |
(在 C++98 中已弃用) (在 C++26 中已移除) |
|
如果流使用动态分配的数组用于输出,则禁用 (flag == true) 或启用 (flag == false) 缓冲区的自动分配/释放。实际上调用 rdbuf()->freeze(flag).
内容 |
[编辑] 注释
调用 str() 后,动态流将自动冻结。在退出创建此 ostrstream 对象的范围之前,需要调用 freeze(false),否则析构函数将导致内存泄漏。此外,一旦达到分配缓冲区的末尾,向冻结流的额外输出可能会被截断。
[编辑] 参数
flag | - | 所需状态 |
[编辑] 返回值
(无)
[编辑] 示例
运行此代码
#include <iostream> #include <strstream> int main() { std::ostrstream 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::ostrstream 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 的公共成员函数) |