“stringstream">“something"的返回值是什么? [英] What is the return value of "stringstream>>something"?

查看:42
本文介绍了“stringstream">“something"的返回值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

stringstream>的返回值是什么?什么?例如,stringstream_obj>>的返回值.int_obj.我知道返回类型仍然是流,因为 istream&运营商>>(int& val).但有什么价值呢?具体来说,这是我的代码.

What is the return value of the stringstream >> something? For example, the return value of stringstream_obj >> int_obj. I understand the return type is still the stream because of istream& operator>> (int& val). But what's the value? To be specific, here is my code.

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main (void)
{
    stringstream sss(string("0"));
    int num;
    if (sss >> num) // don't understand here
        cout << "true" << endl;
    else 
        cout << "false" << endl;
    //output "true"    
    return 0;
}

作为评论,为什么输出是真的?sss 仅包含 1 个字符.一次"ss>>num",返回的stringstream"应该有空内容,因此括号的值应该是假的.

As the comment, why is the output true? The sss contains only 1 character. Once "sss >> num", the returned "stringstream" should have empty content thus the value of the bracket should be false.

赞赏,

推荐答案

operator>> 返回对流的引用,如您所说.然后,在 if 的上下文中,流通过 转换运算符 给出:

operator>> returns a reference to the stream, as you said. Then, in the context of the if the stream is converted to a bool through the conversion operator which gives :

如果流没有错误则为真,否则为假.

true if the stream has no errors, false otherwise.

您成功读取了0",因此流没有错误,再次尝试读取一个,您将看到流有错误并且 if(sss) 评估为 false.

You successfully read the '0' so the stream has no errors, try reading one again and you'll see the stream has errors and if(sss) evaluates to false.

这篇关于“stringstream">“something"的返回值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆