std::strstream::freeze
來自 cppreference.com
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 的公有成員函式) |