cin和cin.get()对于char数组的区别 [英] Difference between cin and cin.get() for char array

查看:137
本文介绍了cin和cin.get()对于char数组的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个代码:

char a[256];
cin>>a;
cout<<a;

char a[256];
cin.get(a,256);cin.get();
cout<<a;

,也许相对于没有cin.get()的第二个;

char a[256];
cin.get(a,256);
cout<<a;

我的问题是(第一个):对于char数组,我应该使用什么? cin或cin.get()?以及为什么我应该使用cin.get();字符初始化后没有参数?

我的第二个问题是:我的c ++老师教我每次使用cin.get()进行初始化char,然后在每个初始化char数组或int数组或仅使用int或其他之后,再次使用cin.get();之后.那是我最初想问的.

所以,现在我得到了这些2: 在这种情况下,在整数初始化后如果没有cin.get(),我的程序将中断,并且我无法再进行char初始化.

int n;
cin>>n;
char a[256];
cin.get(a,256); cin.get();  // with or without cin.get();?
cout<<a;

和正确的一个:

int n;
cin>>n; cin.get();
char a[256];
cin.get(a,256); cin.get(); // again, with or without?
cout<<a;

那怎么了?请有人为每种情况解释!谢谢.

解决方案

它们会做不同的事情,因此,请选择您想要做的事,或者下面给出的更好的选择.

第一个cin>>a;读取一个单词,跳过任何前导空格字符,并在遇到空格字符(包括行尾)时停止.

第二个cin.get(a,256);cin.get();读取整行,然后使用行尾字符,以便重复此操作将读取下一行. cin.getline(a,256)是一种稍微整洁的方法.

第三个cin.get(a,256)读取整行,但在流中保留行尾字符,因此重复此操作将不再提供任何输入.

在每种情况下,如果输入的单词/行长于固定大小的缓冲区,则会出现某种不良行为.因此,通常应使用更友好的字符串类型:

std::string a;
std::cin >> a;              // single word
std::getline(std::cin, a);  // whole line

字符串将增长以容纳任何数量的输入.

I have these 2 codes:

char a[256];
cin>>a;
cout<<a;

and

char a[256];
cin.get(a,256);cin.get();
cout<<a;

and maybe, relative to the second one without cin.get();

char a[256];
cin.get(a,256);
cout<<a;

My question is (first one) : for a char array, what should i use? cin or cin.get()? And why should i use cin.get(); with no parameter after my char initialisation?

And my second question is: my c++ teacher taught me to use every time cin.get() for initialisation chars and AFTER every initialisation char array or int array or just int or whatever, to again put cin.get(); after it. That's what i wanted to ask initially.

So, now i got these 2: In this case, without cin.get() after the integer initialisation, my program will break and i can't do anymore my char initialisation.

int n;
cin>>n;
char a[256];
cin.get(a,256); cin.get();  // with or without cin.get();?
cout<<a;

And the correct one:

int n;
cin>>n; cin.get();
char a[256];
cin.get(a,256); cin.get(); // again, with or without?
cout<<a;

So, what's the matter? Please someone explain for every case ! Thank you.

解决方案

They do different things, so choose whichever does what you want, or the better alternatives given below.

The first cin>>a; reads a single word, skipping over any leading space characters, and stopping when it encounters a space character (which includes the end of the line).

The second cin.get(a,256);cin.get(); reads a whole line, then consumes the end-of-line character so that repeating this will read the next line. cin.getline(a,256) is a slightly neater way to do this.

The third cin.get(a,256) reads a whole line but leaves the end-of-line character in the stream, so that repeating this will give no more input.

In each case, you'll get some kind of ill behaviour if the input word/line is longer than the fixed-size buffer. For that reason, you should usually use a friendlier string type:

std::string a;
std::cin >> a;              // single word
std::getline(std::cin, a);  // whole line

The string will grow to accommodate any amount of input.

这篇关于cin和cin.get()对于char数组的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆