我想清除并重用ostringstream(和底層緩沖區),這樣我的應用程序就不必做太多分配了。如何將對象重置為其初始狀態?
3 回答

慕田峪9158850
TA貢獻1794條經驗 獲得超7個贊
如果要以某種方式清除緩沖區,使其在首次使用之前被清除,則需要先使用MSVC向緩沖區中添加一些內容。
struct Foo {
std::ostringstream d_str;
Foo() {
d_str << std::ends; // Add this
}
void StrFunc(const char *);
template<class T>
inline void StrIt(const T &value) {
d_str.clear();
d_str.seekp(0); // Or else you'll get an error with this seek
d_str << value << std::ends;
StrFunc(d_str.str().c_str()); // And your string will be empty
}
};

守著星空守著你
TA貢獻1799條經驗 獲得超8個贊
考慮一下用例,其中代碼遍歷輸入數據,寫入ostringstream
(基于讀取的數據),然后必須ostringstream
不時寫入某個地方(例如,在讀取某些字符序列之后)內置的字符串,然后開始建立一個新的字符串。
- 3 回答
- 0 關注
- 528 瀏覽
添加回答
舉報
0/150
提交
取消