stringstream是一个用于字符串流处理的类,可以方便地进行字符串的输入输出操作。在C++中,我们可以通过包含头文件来使用stringstream类。下面是一个简单的示例示范了如何使用stringstream:
#include
#include
int main() {
std::stringstream ss;
int num = 123;
ss << num class="hljs-comment">// 将浮点数写入stringstream
double d = 3.14;
ss << " " << d class="hljs-comment">// 将字符串写入stringstream
std::string str = "Hello, World!";
ss << " " << str class="hljs-comment">// 从stringstream中读取数据并输出
int num2;
double d2;
std::string str2;
ss >> num2 >> d2 >> str2;
std::cout << "num2: " << num2 class="hljs-string">"d2: " << d2 class="hljs-string">"str2: " << str2 class="hljs-keyword">return 0;
}
以上代码将输出:
num2: 123
d2: 3.14
str2: Hello, World!
通过stringstream类,我们可以方便地进行各种不同类型数据之间的转换和操作。