源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C++中getline()和get()的方法浅析

  • 时间:2021-11-19 12:52 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C++中getline()和get()的方法浅析
[b]最原始的方法:[/b] 获取输入流最原始的形式就是[code]cin>>(type) [/code],但是这种形式在碰到输入中有空格、制表符或者换行符的时候就会中断,值得注意的是中断后空格、制表符或者换行符还继续留在输入流中。所以最简单的,我们无法使用[code]cin>>(type)[/code]的形式来读取包含空格的字符串,比如输入流中有一句:How are you?使用[code]cin>>(type)[/code]是无法一次性读取出来的,鉴于此,[code]getline()[/code]方法和[code]get()[/code]方法便诞生了。 [b]getline()方法:[/b] [code]getline()[/code]方法读取整行,他使用通过回车键输入的换行符来中断,[code]getline()[/code]方法有两个参数,第一个参数用来存储输入行的数组的名称,第二个参数用来表示读取字符数的大小。[code]getline(name,size)[/code]的方法的使用过程如下:       1. 从输入流中读取一个字符。       2. 如果读取数量达到size-1,将该字符存储到name数组,删除输入流中的该字符,跳转到5。       3. 如果该字符是换行符,删除输入流中的该字符,跳转到5。       4. 否则,将该字符存储到name数组,删除输入流中的该字符,跳转到1。       5. 在name中结尾添加空字符,结束。 下面的代码是使用原始方法和[code]getline()[/code]方法的比较:
#include <iostream>
using namespace std;
int main()
{
 const int arrayLength = 20;
 char name1[arrayLength];
 char name2[arrayLength];
 cout<<"Enter your name1:\n";
 cin>>name1;
 cout<<"Enter your name2:\n";
 cin.getline(name2,arrayLength);
 cout<<"name1: "<<name1<<endl;
 cout<<"name2: "<<name2<<endl;
 cin.get();
 return 0;
}
[img]http://files.jb51.net/file_images/article/201610/20161026102656395.jpg?2016926102818[/img] [b]分析:[/b]我们在输入流中输入name1 name2 name3,然后[code]cin>>name1[/code]会读取name1,因为name1后面是空格,但是空格符是保留的,因为在name2中读取的结果是” name2 name3”。 [b]get()方法:[/b] [code]get()[/code]的参数和使用方法与[code]getline()[/code]方法一致,唯一的区别就是[code]get()[/code]方法在碰到换行符是不对输入流中的换行符进行删除。这样我们读取输入流的过程就会产生一个问题,怎么跳过换行符,幸运的是[code]get()[/code]方法提供了一种变体,[code]cin.get()[/code]读取下一个字符,包括换行符,下面的例子掩饰了[code]cin.get(name,size)[/code]和[code]cin.get()[/code]的使用:
#include <iostream>
using namespace std;
int main()
{
 const int arrayLength = 40;
 char name1[arrayLength];
 cout<<"Enter your name1:\n";
 cin.get(name1,arrayLength);
 cin.get();
 cout<<"name1: "<<name1<<endl;
 cin.get();
 return 0;
}
运行结果: [img]http://files.jb51.net/file_images/article/201610/20161026102852724.jpg?201692610294[/img] [b]getline()和get()方法读取空行的问题:[/b] 所谓空行,就是输入流中只有换行符,当[code]getline()[/code]和[code]get()[/code]方法碰到空行时,会设置失效位,使后面所有的输入都中断,我们分析下面的代码:
#include <iostream>
using namespace std;
int main()
{
 const int arrayLength = 40;
 char name1[arrayLength];
 char name2[arrayLength];
 cout<<"Enter your name1:\n";
 cin.get(name1,arrayLength);
 cout<<"Enter your name2:\n";
 cin.get(name2,arrayLength);
 cout<<"name1: "<<name1<<endl;
 cout<<"name2: "<<name2<<endl;
 cin.get();
 cin.get();
 return 0;
}
比如我们输入this is name1,回车,这时name1中读取的是this is name1,接下来遇到回车就中断了,在接下来name2读取的时候输入流就成为空行了,这就导致后面的[code]cin.get()[/code]都没有了效果,也就是上面程序总会一闪而过。因为[code]get()[/code]方法碰到了中断导致所有的输入都中断。碰都这种问题的解决方法是在读取输入流之前调用[code]cin.clear()[/code]方法来恢复输入。
#include <iostream>
using namespace std;
int main()
{
 const int arrayLength = 40;
 char name1[arrayLength];
 char name2[arrayLength];
 cout<<"Enter your name1:\n";
 cin.get(name1,arrayLength);
 cout<<"Enter your name2:\n";
 cin.get(name2,arrayLength);
 cout<<"name1: "<<name1<<endl;
 cout<<"name2: "<<name2<<endl;
 cin.clear();
 cin.get();
 cin.get();
 return 0;
}
[img]http://files.jb51.net/file_images/article/201610/20161026103124513.jpg?2016926103841[/img] [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用PHP能有所帮助,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部