//注:如果stream中的字符串没有全部读取完,可以用str("")来清空,所以以防万一,最好应用的时候同时使用clear和str。
#include <sstream>
#include <iostream>
int main()
{
std::stringstream stream;
int first, second;
char t[6];
stream << "456c"; //插入字符串
stream >> first; //转换成int
/*这个时候stream已经是eofbit状态了 所以接下来需要进行clear才能继续往stream写入数据*/
std::cout << first << std::endl;
stream.str("");
stream.clear(); //在进行多次转换前,必须清除stream
stream << true; //插入bool值
stream >> second; //提取出int
std::cout << second << std::endl;
system("pause");
}
c++学习系列:stringstream的用法
最新推荐文章于 2024-04-11 15:50:23 发布