在线文档教程
C++
输入/输出 | Input/output

std::ostrstream::freeze

STD::发情期:冻结

void freeze(bool flag = true

如果流使用动态分配的数组进行输出,则禁用%28。flag == true%29或启用%28flag == false%29缓冲区的自动分配/取消分配。有效呼叫rdbuf()->freeze(flag)...

注记

在打电话给str()动态流自动冻结。打电话给freeze(false)在退出ostrstream对象被创建。否则,析构函数将泄漏内存。另外,当被冻结的流到达分配缓冲区的末尾时,可能会截断它的额外输出。

参数

flag-desired status

返回值

%280%29

二次

#include <strstream> #include <iostream> 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"

二次

另见

freezesets/clears the frozen state of the buffer (public member function of std::strstreambuf)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/ostrstream/冻结