服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - c++中的两种getline用法详解

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

2021-08-16 14:40IT_xiaolaoshu C/C++

c++中有2种getline函数,一种在头文件 中,是istream类的成员函数;另一种是在头文件 中,是普通函数。这篇文章主要介绍了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函数两种重载形式:

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

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

例子代码:

  1. // istream::getline example
  2. #include <iostream> // std::cin, std::cout
  3. int main () {
  4. char name[256], title[256];
  5. std::cout << "Please, enter your name: ";
  6. std::cin.getline (name,256);
  7. std::cout << "Please, enter your favourite movie: ";
  8. std::cin.getline (title,256);
  9. std::cout << name << "'s favourite movie is " << title;
  10. return 0;
  11. }

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

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

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

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

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

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

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

结果:

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

第二点,代码如下

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

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

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

 

  1. (1)
  2. istream& getline (istream& is, string& str, char delim);
  3. istream& getline (istream&& is, string& str, char delim);
  4.  
  5. (2)
  6.  
  7. istream& getline (istream& is, string& str);
  8. istream& getline (istream&& is, string& str);

说明:

  • is:表示一个输入流,例如 cin。
  • str:用来存储输入流中的信息
  • delim:自定义结束字符,默认是 '\n '

例子代码:

  1. #include<iostream>
  2. #include<string>
  3. int main()
  4. {
  5. std::string name; //这里定义的是string类型,而不是char
  6. std::getline(std::cin,name);
  7. std::cout<<name<<std::endl;
  8.  
  9. return 0;
  10. }

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

不设置终止符

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string name;
  7. while(getline(cin,name))
  8. {
  9. cout<<name<<endl;
  10. }
  11. return 0;
  12. }

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

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string name;
  7. while(getline(cin,name,'#'))
  8. {
  9. cout<<"输出结果:"<<endl;
  10. cout<<name<<endl;
  11. }
  12.  
  13. return 0;
  14. }

结果:

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

参考链接:

1.istream中的getline

2.string头文件中的getline

总结

以上所述是小编给大家介绍的c++中的两种getline用法详解,希望对大家有所帮助,也非常感谢大家对我们网站的支持!

原文链接:https://blog.csdn.net/Big_laoshu/article/details/79345351

延伸 · 阅读

精彩推荐