getline(): how many kinds]C++std library

See also

(Note All the getline() functions below are designed to return text from a File or cin — actually a one-way stream. Inapplicable to sockets.)

1) Most documentations mention the older c-str-based istream::getline() However,

2) is a std::getline(). Unlike the c-str versions of istream::getline() method, this std::string version is implemented as free function instead of member function in istream class.

This version is more modern. C++11 added new features to this free function, but didn’t bother with (1)

I feel cin can emulate it with for(; !cin.eof(); cin>>str3){…}. Of course, cin can also parse numbers!

Differences between these 2 getline() functions:
– One uses c-str; the other uses std::string class.
– One is a member function; the other a free function

Similarities —
– both require streams, therefore unusable in C. discusses both.

There’s an IKM question on the (older) member function (1).

3) describes a prehistoric ANSI-C standard library function — not using std::string Class or std::istream Class. is a short and sharp tutorial on it.

——- philosophically ——-
We can’t assume the most “everyday” programming tasks in a major language are always well-covered online — completely covered, with the confusing details pointed out. Reading a text file is basic but supported by a confusing bunch of alternatives, all named getline(). does describe pitfalls of istream::getline()


Fill in your details below or click an icon to log in: 徽标

您正在使用您的 账号评论。 登出 /  更改 )

Google photo

您正在使用您的 Google 账号评论。 登出 /  更改 )

Twitter picture

您正在使用您的 Twitter 账号评论。 登出 /  更改 )

Facebook photo

您正在使用您的 Facebook 账号评论。 登出 /  更改 )

Connecting to %s