简体   繁体   中英

istream function parameter vs inline std::cin

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();
}
  1. 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 .

  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM