C++中的getline函数

C++中本质上有两种getline函数,一种在头文件< istream >中,是istream类的成员函数。一种在头文件< string >中,是普通函数。
一、在< istream >中的getline函数有两种重载形式:

istream& getline (char* s, streamsize n );

istream& getline (char* s, streamsize n, char delim );
作用是从istream中读取至多n个字符保存在s对应的数组中。即使还没读够n个字符,如果遇到换行符’\n’(第一种形式)或delim(第二种形式),则读取终止,’\n’或delim都不会被保存进s对应的数组中。

二、在< string >中的getline函数有四种重载形式:
istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);

用法和上一种类似,不过要读取的istream是作为参数is传进函数的。读取的字符串保存在string类型的str中。

发布了94 篇原创文章 · 获赞 193 · 访问量 5582

猜你喜欢

转载自blog.csdn.net/weixin_45884316/article/details/104174762