【发布时间】:2022-01-09 23:02:08
【问题描述】:
如何将 cout 的输出存储到字符串或字符类型的变量中?
我已经编写了以下代码,但它不起作用:
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
string n;
n = (cout<<"\nHello world");
cout<<n;
return 0;
}
【问题讨论】:
-
为什么不先将字符串存储在
n,然后再存储cout << n? -
您应该使用
std::ostringstream而不是std::cout。