if(cin)的用途? [英] Purpose of if(cin)?

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

问题描述

我在这里要求一些帮助理解加速c ++中提出的一些概念。这是我遇到的问题。

I'm here to ask for some help in understanding some concepts presented in accelerated c++. Here is where I am having a problem.

vector<Student_info> students;  //Student_info is a structure we made
Student_info record;
string::size_type maxlen = 0;


// read and store all the records, and find the length of the longest name
while (read(cin, record)) { //Here is where I find hard to follow
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}

read()的定义:

Definition of read():

istream& read(istream& is, Student_info& s)
{
// read and store the student's name and midterm and final exam grades
is >> s.name >> s.midterm >> s.final;
read_hw(is, s.homework); // read and store all the student's homework grades
return is;
}

read_hw()的定义:

definition of read_hw():

// read homework grades from an input stream into a vector<double>
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear() ;
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}

到目前为止我的理解是,当我们调用: while(read(cin,record))我们要求一个字符串: record.name double 值: record.midterm record.final 。在我们的用户输入数据之后,我们将输入我们的 read_hw()函数,我们将清除我们的向量中可能存在的任何以前的元素。然后我们输入并记录一个值流到一个称为 record.homework 的家庭作业成绩矢量。我想我很了解。如果没有,请让我知道。

The way I understand it so far is that when we call: while (read(cin,record)) we ask for a string: record.name, and two double values: record.midterm and record.final. After our user has inputted that data, we will enter our read_hw() function where we will clear any previous elements that may have been in our vector. Then we input and record a a stream of values into a vector of homework grades called record.homework. I think I am understanding that much. If not, please let me know.

我不明白我们的函数如何知道什么时候返回,因此返回到我们的第一个,而循环。编辑:啊,用户必须做文件结束通知两次才能结束程序。所以我想我回答我自己的问题...但是有人能够解释在 read_hw()中的 if(in) / code>函数,这将是非常有益的。这将是 if(cin),那么等待我们的用户输入数据?或者 if(cin)自动为true? (为什么?)

I don't understand how our function knows when to return in, and therefore return back to our first while loop. Ah, the user has to make and end-of-file notification twice to end the program. So I suppose I answered my own question... But would someone be able to explain the purpose of if(in) in our read_hw() function, that would be very helpful. It would be if(cin), so does that wait for our user to input data? Or is if(cin) automatically true? (why?)

推荐答案

if(cin) wait - 它检查错误。只有运算符>>等待输入。在这种情况下, while(in>> x)执行读取和等待。它在while循环条件内的原因是因为之后在>> x 完成它返回对in的引用,隐式转换为bool,用于发出错误,如 if(cin)

No if(cin) doesn't wait - it checks for errors. Only the operator >> waits for input. In this case while(in >> x) does the reading and waiting. The reasons it is inside a while loop condition is because after in >> x finishes it returns a reference to in, which is implicitly converted to a bool, which is used to signal an error like if(cin).

在这两种情况下,如果没有错误,cin将为true,如果有错误则为false。

In both cases, cin will be true if there has been no error, and false if there has been an error.

可以更好地了解发生了什么,但是可能不是欢迎。基本上,每当一个istream被当作一个bool处理,它将是真的没有错误,如果有错误,则为false。

This gives a better idea of what is going on, but might be not be to welcoming. Basically whenever an istream is treated like a bool, it will be true is there was no error, and false if there has been an error.

基本上, istream(调用它foo)到bool总是!foo.fail()。所以它是fail()的相反。这意味着有两种方式可以实现(直接从表此处

Basically, the conversion of an istream (call it foo) to bool is always !foo.fail(). So it is the opposite of fail(). This means there are two ways it can be true (straight from the table here:


  1. i / o操作时出现逻辑错误(读取错误类型等)

  2. /对i / o操作的写错误(不可预测的东西)

所以文件末尾不包括

如果一个istream是false,你可以使用bad()来区分1和2,如果bad是true,情况2,如果bad()为false,则为case 1.(还有其他方法,看看rdstate())

If an istream works out as false, you can distinguish between 1 and 2 using bad(). If bad is true it was case 2, if bad() is false, it will be case 1. (There are other ways as well, look at rdstate())

这篇关于if(cin)的用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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