C ++使用stringstream后<<作为参数 [英] C++ Using stringstream after << as parameter
问题描述
是否可以编写一个接受字符串流的方法,并使其看起来像这样:
void method(string str)
void printStringStream(StringStream& ss)
{
method(ss.str
}
可以像这样调用
stringstream var;
printStringStream(var<<Text<< intVar<<More text<< floatvar);
我查找了<运算符,它看起来像返回一个 ostream&
对象,但我可能读取 此
错误或只是无法正确实施。 >
真正我想要的是一个干净的方式,将字符串连接在一起并传递给函数。我能找到的最干净的事情是一个stringstream对象,但仍然有很多不足之处。
注意:
c> c ++ 11 答案,因为我在Visual Studio 2010上运行(根据我的意愿,但仍然)
Boost
所以要坚持下去。
我不会反对一个自定义方法,只要它清理这个混乱。
编辑:
PiotrNycz语法我实现了我写这样的代码的目标,
try {
// code
} catch(exception e)
{
printStringStream(stringstream()<<发生异常。\\\
<<Error:<< e.message
<<\\\
如果此问题仍然存在,请联系<< contactInfo
< \\ n对不便,我们深表歉意);
}
这是我所希望的那样干净,可读。
希望这有助于其他人清理写信息。
,带我一分钟。由于运算符<<
是为所有 ostream类型重载的自由函数,因此不会返回 std :: stringstream
,它像你说的那样返回 std :: ostream
。
void printStringStream(std :: ostream& ss)
ostream
没有 .str()
成员,但他们有一种将整个流复制到另一个流的奇妙方式:
std :: cout< ss.rdbuf();
这是一个指向完整代码的链接,显示它编译和运行 http://ideone.com/DgL5V
EDIT
如果你真的需要一个字符串的函数,我可以想到几个解决方案:
首先, p>
stringstream var;
var<< Text<< intVar<< More text<<< floatvar;
printStringStream(var);
其次:将流复制到字符串(可能的性能问题)
void printStringStream(ostream& t)
{
std :: stringstream ss;
ss<< t.rdbuf();
method(ss.str());
}
第三步:使另一个函数接受流
Is it possible to write a method that takes a stringstream and have it look something like this,
void method(string str)
void printStringStream( StringStream& ss)
{
method(ss.str());
}
And can be called like this
stringstream var;
printStringStream( var << "Text" << intVar << "More text"<<floatvar);
I looked up the << operator and it looks like it returns a ostream&
object but I'm probably reading this
wrong or just not implementing it right.
Really all I want is a clean way to concatenate stuff together as a string and pass it to a function. The cleanest thing I could find was a stringstream object but that still leaves much to be desired.
Notes:
I can't use much of c++11
answers because I'm running on Visual Studio 2010 (against my will, but still)
I have access to Boost
so go nuts with that.
I wouldn't be against a custom method as long as it cleans up this mess.
Edit:
With @Mooing Duck's answer mixed with @PiotrNycz syntax I achieved my goal of written code like this,
try{
//code
}catch(exception e)
{
printStringStream( stringstream() << "An exception has occurred.\n"
<<" Error: " << e.message
<<"\n If this persists please contact "<< contactInfo
<<"\n Sorry for the inconvenience");
}
This is as clean and readable as I could have hoped for.
Hopefully this helps others clean up writing messages.
Ah, took me a minute. Since operator<<
is a free function overloaded for all ostream types, it doesn't return a std::stringstream
, it returns a std::ostream
like you say.
void printStringStream(std::ostream& ss)
Now clearly, general ostream
s don't have a .str()
member, but they do have a magic way to copy one entire stream to another:
std::cout << ss.rdbuf();
Here's a link to the full code showing that it compiles and runs fine http://ideone.com/DgL5V
EDIT
If you really need a string in the function, I can think of a few solutions:
First, do the streaming seperately:
stringstream var;
var << "Text" << intVar << "More text"<<floatvar;
printStringStream(var);
Second: copy the stream to a string (possible performance issue)
void printStringStream( ostream& t)
{
std::stringstream ss;
ss << t.rdbuf();
method(ss.str());
}
Third: make the other function take a stream too
这篇关于C ++使用stringstream后<<作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!