<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。
结果:
1.stringstream::str(); returns a string object with a copy of the current contents of the stream.
2.stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.
3.stringstream清空,
stringstream s;
s.str("");
4.实现任意类型的转换
#include <sstream>
void test_stringstream()
{
string tmpStr;
vector<int> temp_vals;
temp_vals.push_back(1);
temp_vals.push_back(3);
temp_vals.push_back(5);
tmpStr += ".temp";
for (int i = 0; i < temp_vals.size(); i++ )
{
stringstream ss;
ss << temp_vals[i];
if (i == 0)
tmpStr = tmpStr + " " + ss.str();
else
tmpStr = tmpStr + ", " + ss.str();
}
}
结果:
tmpStr = .temp 1, 3, 5