std::ostrstream::freeze
來自 cppreference.com
< cpp | io | ostrstream
void freeze( bool flag = true ); |
(C++98 起棄用) (C++26 中移除) |
|
如果流使用動態分配的陣列進行輸出,則停用 (flag == true) 或啟用 (flag == false) 緩衝區的自動分配/釋放。實際上呼叫 rdbuf()->freeze(flag)。
目錄 |
[edit] 注意
在呼叫 str() 後,動態流會自動凍結。在此 ostrstream 物件建立的作用域退出之前,需要呼叫 freeze(false),否則解構函式將導致記憶體洩漏。此外,對已凍結流的額外輸出一旦達到分配緩衝區的末尾,可能會被截斷。
[edit] 引數
flag | - | 期望的狀態 |
[edit] 返回值
(無)
[edit] 示例
執行此程式碼
#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"
[edit] 參閱
設定/清除緩衝區的凍結狀態 ( std::strstreambuf 的公共成員函式) |