stringstream可以吞下不同的类型,然后吐出不同的类型。
这样可以实现int,string,double等类型的转换
1 #include<sstream> 2 3 using namespace std; 4 5 int main() 6 { 7 int x; 8 stringstream temp; 9 temp << x;//吞入被转换的类型 10 string s1 = temp .str();//吐出需要的类型 11 } 12
缺点:效率低,运行时间长;由于cin,cout为了兼容C有个同步流。关闭同步流的方法
看来stringstream似乎不打算主动释放内存(或许是为了提高效率),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消耗,因些这时候,需要适时地清除一下缓冲 (用 stream.str("") )。
另外不要企图用 stream.str().resize(0),或 stream.str().clear() 来清除缓冲,使用它们似乎可以让stringstream的内存消耗不要增长得那么快,但仍然不能达到清除stringstream缓冲的效果(不信做个实验就知道了,内存的消耗还在缓慢的增长!)
参考文章:
https://blog.csdn.net/u014097230/article/details/52089530