while(std :: cin>>值)如何工作? [英] How does while (std::cin >> value) work?

查看:60
本文介绍了while(std :: cin>>值)如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ Primer中给出的示例中,

In an example given in C++ Primer,

#include <iostream>
using namespace std;

int main() {
    int sum = 0, value = 0;  
    while (std::cin >> value) {       
        sum += value; // equivalent to sum = sum + value
    }    
    std::cout << "Sum is: " << sum << std::endl;    
    return 0; 

}

(std :: cin >>值)如何返回true?什么是文件结尾"?看来我必须理解该术语才能理解我的主要问题.

How does (std::cin >> value) return true? And what is an "End Of File"? It seems that I must understand that term in order to understand my primary question.

谢谢!

推荐答案

已重载的 operator>> 函数返回对该流本身的引用,并且该流具有 an重载的运算符,该运算符可以在布尔条件下使用它,以查看上一个操作是否正常.部分可以或不可以"包括到达文件末尾或其他错误.

The overloaded operator>> function returns a reference to the stream itself, and the stream have an overloaded operator that allows it to be used in a boolean condition to see if the last operation went okay or not. Part of the "okay or not" includes end of file reached, or other errors.

这篇关于while(std :: cin&gt;&gt;值)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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