How these two function differs? Is there any different kinds of istream other than std::cin and what is the point of returning the istream?
using namespace std;
istream& readInput(istream& in, vector<string>& wordList)
{
string word;
while (in >> word)
{
wordList.push_back(word);
}
in.clear();
return in;
}
void readInput(vector<string>& wordList)
{
string word;
while (cin >> word)
{
wordList.push_back(word);
}
cin.clear();
}
cin
is absolutely not the only one istream
, I think that it is not even the most used one. Read about eg ifstream
and istringstream
.
Returning istream&
from the function may be useful in various situations - it depends on the context. For just simple calls of readInput
one can skip it and make the function void
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.