Implementation of getline ( istream& is, string& str )

前端 未结 3 661
栀梦
栀梦 2020-12-10 07:35

My Question is very simple, how is getline(istream, string) implemented? How can you solve the problem of having fixed size char arrays like with getline (char* s, streamsiz

3条回答
  •  再見小時候
    2020-12-10 07:45

    getline(istream&, string&) is implemented in a way that it reads a line. There is no definitive implementation for it; each library probably differs from one another.

    Possible implementation:

    istream& getline(istream& stream, string& str)
    {
      char ch;
      str.clear();
      while (stream.get(ch) && ch != '\n')
        str.push_back(ch);
      return stream;
    }
    

提交回复
热议问题