【问题标题】:strstream in c++c++中的strstream
【发布时间】:2011-05-08 14:00:57
【问题描述】:

我正在写代码

#include<sstream>
#include<iostream>

using namespace std;
int main(){
strstream temp;

int t =10;
temp>>10;

string tt ="testing"+temp.str();

有一个问题,它对临时变量根本不起作用,只是得到结果只有字符串测试,最后没有10?

}

【问题讨论】:

  • 这能编译吗? strstream 在 而不是 . 中定义

标签: visual-c++ strstream


【解决方案1】:

由于您已经包含了sstream,我想您已经想到了ostringstream 类。

ostringstream temp;
int i = 10;
temp << i;
string tt = "testing" + temp.str();

要使用strstream,请包含&lt;strstream&gt;strstreamchar* 一起使用,它们是 C 字符串。使用ostringstream 处理basic_string 类型的对象。

【讨论】:

  • 不,还是同样的问题,会不会是编译器的问题?
【解决方案2】:

您应该改用operator&lt;&lt;()temp &lt;&lt; 10;

【讨论】:

    【解决方案3】:

    这个问题(在我看来)就像一个简单的错字。您需要将:temp&gt;&gt;10; 替换为 temp&lt;&lt;10;

    【讨论】:

    • 认为我没有这样做...只是一些错误...它应该可以工作但不知何故没有:(
    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多