#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main (int argc,char *argv[])
{
//定义一个stringstream对象sstr,并用a填充字符串缓冲区
stringstream sstr("a");
//调用put函数往字符串缓冲区插入一个字符b,插入位置是开头,故将a覆盖掉了
sstr.put('b');
//再次插入一个字符c
sstr.put('c');
//调用插入运算符函数插入一个字符串de
sstr<<"de";
//调用str函数将字符串缓冲区中的内容转换为string型字符串并返回,返回值初始化了string型字符型str
string str=sstr.str();
//输出字符串,结果为:bcde
cout<<"string:"<<str<<endl;
char ch;
//调用提取运算符函数将字符串缓冲区的第一个字符提取到变量ch中
sstr>>ch;
//输出结果为:b
cout<<"char:"<<ch<<endl;
return 0;
}
字符串输入/输出流类stringstream
最新推荐文章于 2024-04-26 13:54:41 发布