stringstream使用小结

1.头文件:#include<sstream>

2.stringstream是C++提供的串流(stream)物件

3、clear()重置流的标志状态;str()清空流的内存缓冲,重复使用内存消耗不再增加!

 1 #include<string>
 2 #include<sstream>
 3 #include<iostream>
 4 using namespace std;
 5 int main()
 6 {
 7     stringstream sstream;//定义流
 8     sstream << "first" << " " << "string,";
 9     sstream << " second string";
10     cout << "strResult is: " << sstream.str() << endl;//str()方法能够将流转换为string,但这不影响原sstream
11     sstream.str("");//清空内存
12     sstream << "third string";
13     cout << "After clear, strResult is: " << sstream.str() << endl;
14     return 0;
15 }
posted @ 2020-01-22 20:39  SavenNeer  阅读(218)  评论(0编辑  收藏  举报