- 直接初始化
// #include<sstream>
void stringstreamtest(){
string s = "First, Second, Third";
stringstream ss(s); //直接初始化
string word;
while(ss >> word)
cout<<word;
//输出:
//First,Second,Third
cout<<endl;
}
- 输入输出
// #include<sstream>
void stringstreamtest(){
stringstream ss;
string s = "abcde";
int i = 12345;
char c = '#';
ss << s << i <<c;
cout<<ss.str();
//输出:
//abcde12345#
cout<<endl;
}
注意:初始化和操作符“<<”不能共用,除非有必要这样做。
本文介绍了如何使用 C++ 中的 stringstream 类进行字符串的直接初始化及输入输出操作。通过两个实例展示了初始化方式及其对输出的影响,并强调了初始化与操作符‘<<’的使用规范。

1万+

被折叠的 条评论
为什么被折叠?



