c++中的两种getline用法详解分享!

getline是C++标准库函数;但不是C标准库函数,而是POSIX(IEEE Std 1003.1-2008版本及以上)所定义的标准库函数(在POSIX IEEE Std 1003.1-2008标准出来之前,则只是GNU扩展库里的函数)。getline会生成一个包含一串从输入流读入的字符的字符串,直到以下情况发生会导致生成的此字符串结束:1)到文件结束,2)遇到函数的定界符,3)输入达到最大限度。

getline()函数是一个比较常见的函数。根据免费精选名字大全直接”望文->生义”,就知道这个函数是来完成读入一行数据。

下面就对C++ — getline()函数的用法说明,以及getline()函数作为while条件的问题,总结一下:

在C++中本质上有两种getline函数,(称为第一种)一种在头文件<istream>中,是istream类的成员函数。

好了,看正文,给大家详细介绍。

功能:读入一行数据。

c++中有2种getline函数,一种在头文件 <istream> 中,是istream类的成员函数;另一种是在头文件 <string> 中,是普通函数。

1.在头文件 <istream> 的getline函数两种重载形式:

  istream& getline (char* s, streamsize n );//读取最多n个字符保存在s对应的数组中,即使大小不够n,  istream& getline (char* s, streamsize n, char delim ); //读取最多n个字符保存在s对应的数组中,遇到delim,或者读完一行,或字数达到限制则终止

特别说明: 最多读取n个字符中结束字符算一位。

例子代码:

  // istream::getline example  #include <iostream>  // std::cin, std::cout  int main () {   char name[256], title[256];   std::cout << "Please, enter your name: ";   std::cin.getline (name,256);   std::cout << "Please, enter your favourite movie: ";   std::cin.getline (title,256);   std::cout << name << "'s favourite movie is " << title;   return 0;  }

通过字数限制修改的代码:

c++中的两种getline用法详解

通过设置终止字符使用getline函数的代码:

c++中的两种getline用法详解

关于这个函数的2点疑问:

第一点,当定义一个小的数组,输入的时候要求输入的长度超出数组长度,输出的时候会输出全部的值。

  #include<iostream>  int main()  {  	char name[5];  	std::cin.getline(name,25);  	std::cout<<name<<std::endl;  	return 0;  } 

结果:

c++中的两种getline用法详解

第二点,代码如下

  // istream::getline example  #include <iostream>  // std::cin, std::cout  int main () {   char name[6], title[256];   std::cout << "Please, enter your name: ";   std::cin.getline (name,4);   //代码通过字数限制存入数组后就不再运行11-12行代码,而是直接16行    std::cout << "Please, enter your favourite movie: ";   std::cin.getline (title,256,'#');   std::cout<<std::endl;	   std::cout << name << " " << title;   return 0;  }

c++中的两种getline用法详解

2.在头文件<string>中的getline函数

  (1)   istream& getline (istream& is, string& str, char delim);  istream& getline (istream&& is, string& str, char delim);    (2)     istream& getline (istream& is, string& str);  istream& getline (istream&& is, string& str);

说明:

例子代码:

  #include<iostream>  #include<string>  int main()  {  	std::string name; //这里定义的是string类型,而不是char  	std::getline(std::cin,name);  	std::cout<<name<<std::endl;  	  	return 0;  } 

getline在while语句中作为判定条件:

不设置终止符

  #include<iostream>  #include<string>  using namespace std;  int main()  {  	string name;  	while(getline(cin,name))  	{  		cout<<name<<endl;  	}  	return 0;  } 

使用终止符的while语句(当输入 ‘ n ‘ 也不受影响)

  #include<iostream>  #include<string>  using namespace std;  int main()  {  	string name;  	while(getline(cin,name,'#'))  	{  		cout<<"输出结果:"<<endl;  		cout<<name<<endl;  	}  	  	  	return 0;  } 

结果:

c++中的两种getline用法详解

参考链接:

1.istream中的getline

2.string头文件中的getline

总结

以上所述是小编给大家介绍的c++中的两种getline用法详解,希望对大家有所帮助,也非常感谢大家对<计算机技术网(www.ctvol.com)!!>网站的支持!

—-想了解c++中的两种getline用法详解分享!全部内容且更多的C语言教程关注<计算机技术网(www.ctvol.com)!!>

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/483777.html

(0)
上一篇 2020年11月10日
下一篇 2020年11月10日

精彩推荐