istream& read_hw(istream& in, vector<double>& hw)
{
if(in){
hw.clear();
double x;
while (in >> x)
hw.push_back(x);
in.clear;
}
return in;
}
Can someone please explain this code to me. I have been reading the book Accelerated C++ and until now I have been loving it, but now that I am working with this new example I feel completely lost. This function is supposed to read homework grades from an input stream into a vector, but I am lost at a few places.
What is the variable type "istream" and why are we referencing it instead of just using it?
What is "if(in)" saying, does it mean if the input is successful execute the following code?
Also, I am having a hard time understanding why you need to use references a lot of the time, so if anyone can give me a little insight that would be really helpful to me.
I haven't been able to wrap my head around why you need to reference things if you aren't going to be changing their value at all, such as saying "const vector<(double)>& hw". I thought the whole point of a reference was to change the variable within the function for good.
Can anyone tell me what "return in;" will yield? Will it yield one of the arguments of the function or what?
Thank you so much for the help! It really helps to have somewhere to go as I am just learning C++. :)