C++ cout << 字符串流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8816817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:09:34  来源:igfitidea点击:

cout << stringstream

c++stringstream

提问by MCP

When I put something into a stringstream, let's say a real number, if I then insert that stringstream object into cout...what am I looking at?

当我将一些东西放入 stringstream 时,假设是一个实数,如果我然后将该 stringstream 对象插入 cout ......我在看什么?

Usually I'm getting some strange number. Is this a memory location? Just curious.

通常我会得到一些奇怪的数字。这是内存位置吗?只是好奇。

It looks like the below comment hit it but here's what I'm trying to do:

看起来下面的评论击中了它,但这是我想要做的:

string stringIn; 
stringstream holdBuff;
holdBuff << getline(cin, stringIn);
cout << holdBuff; 

Basically I was just trying to see what holdBuff looked like once I inserted stringIn. I am trying to have the user enter a string and then I want to step through it looking for it's contents and possilbly converting...

基本上,我只是想看看当我插入 stringIn 后,holdBuff 是什么样子的。我试图让用户输入一个字符串,然后我想逐步通过它寻找它的内容并可能转换...

回答by James Kanze

What do you think

你觉得怎么样

holdBuff << getline(cin, stringIn);

is doing. The return type of getlineis a reference to the stream being read (cin) in this case. Since there's no <<defined which takes an std::istreamas second argument, the compiler tries different conversions: in C++11, std::istreamhas an implicit conversion to bool, and in earlier C++, an implicit conversion to std::ios*, or something similar (but the only valid use of the returned value is to convert it to bool). So you'll either output 1(C++11), or some random address (in practice, usually the address of the stream, but this is not guaranteed). If you want to get the results of a call to getlineinto an std::ostringstream, you need two operations (with a check for errors between them):

是在做。在这种情况下,的返回类型getline是对正在读取的流的引用 ( cin)。因为没有<<限定它接受一个std::istream作为第二个参数,编译器试图不同的转换:在C ++ 11,std::istream有一个隐式转换 bool,并且在较早的C ++,一个隐式转换到std::ios*,或类似的东西(但唯一的有效利用的返回值是将其转换为bool)。所以你要么输出1(C++11),要么输出一些随机地址(实际上,通常是流的地址,但这不能保证)。如果你想获得一个调用的结果 getlinestd::ostringstream,则需要两个操作(与它们之间的误差校验):

if ( !getline( std::cin, stringIn ) )
    //  Error handling here...
holdBuff << stringIn;

Similarly, to write the contents of a std::ostringstream,

类似地,要写入 a 的内容std::ostringstream

std::cout << holdBuf.str() ;

is the correct solution. If you insist on using an std::stringstreamwhen an std::ostringstreamwould be more appropriate, you can also do:

是正确的解决方案。如果您坚持使用std::stringstreamwhenstd::ostringstream更合适,您还可以这样做:

std::cout << holdBuf.rdbuf();

The first solution is preferable, however, as it is far more idiomatic.

然而,第一种解决方案更可取,因为它更加地道。

In any case, once again, there is no <<operator that takes any iostreamtype, so you end up with the results of the implicit conversion to boolor a pointer.

在任何情况下,再一次,没有<<任何iostream类型的运算符 ,因此您最终会得到隐式转换为bool或 指针的结果。

回答by Bo Persson

Yes, you are likely to see the address of the stringstream.

是的,您很可能会看到字符串流的地址。

If you want to display the string it contains, try

如果要显示它包含的字符串,请尝试

cout << stream.str();

回答by Goz

Yes it is most likely a memory location of some form or other. Most likely it is the pointer to the stringstream object itself.

是的,它很可能是某种形式的内存位置。它很可能是指向 stringstream 对象本身的指针。

You could confirm this as follows:

您可以通过以下方式确认这一点:

std::stringstream ss;
unsigned long long ll = (unsigned long long)&ss;
cout << ll;

That said when you want to cout a stringstream you should use the str() function as follows:

也就是说,当你想计算一个字符串流时,你应该使用 str() 函数,如下所示:

cout << ss.str();

回答by Michael Krelin - hacker

cout << s.rdbuf();

is what you want. Alternatively you may want to

是你想要的。或者你可能想要

cout << s.str();

which may be more expensive in terms of resources though.

但这在资源方面可能更昂贵。