我想清除并重用ostringstream(和底层缓冲区),这样我的应用程序就不必做太多分配了。如何将对象重置为其初始状态?
                    
                    
                3 回答
 
                    
                    
                            慕田峪9158850
                            
                                
                            
                        
                        
                                                
                    TA贡献1794条经验 获得超8个赞
如果要以某种方式清除缓冲区,使其在首次使用之前被清除,则需要先使用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 关注
- 550 浏览
添加回答
举报
0/150
	提交
		取消
	