简体   繁体   English

char数组的cin和cin.get()的区别

[英]Difference between cin and cin.get() for char array

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();也许,相对于没有 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()? cin 还是 cin.get()? And why should i use cin.get();为什么我应该使用 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();我的第二个问题是:我的 c++ 老师教我每次使用 cin.get() 初始化字符,在每次初始化字符数组或 int 数组或只是 int 或其他之后,再次放置 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.所以,现在我得到了这些 2:在这种情况下,在整数初始化之后没有 cin.get() ,我的程序将中断,我不能再做我的 char 初始化。

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;第一个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();第二个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. cin.getline(a,256)是一种稍微简洁的方法来做到这一点。

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.第三个cin.get(a,256)读取整行,但将行尾字符留在流中,因此重复此操作将不会提供更多输入。

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.字符串将增长以适应任何数量的输入。

The problem, most likely, is in the way you enter the values later on.问题很可能在于您稍后输入值的方式。 The cin.get() after every initialization is there to "grab" the newline character that gets put in the stream every time you press enter.每次初始化后的cin.get()用于“抓取”每次按 Enter 时放入流中的换行符。 Say you start entering your values like this:假设您开始输入这样的值:

2 

a b c d...

Assuming you have pressed enter after 2, the newline character was also put on the stream.假设您在 2 之后按下了 Enter,换行符也被放入流中。 When you call cin.get() after that, it will grab and discard the newline character, allowing the rest of your code to properly get the input.当您在此之后调用cin.get() ,它将抓取并丢弃换行符,从而允许您的其余代码正确获取输入。

To answer your first question, for an array, you should use cin.get instead of the overloaded operator >> cin>> as that would only grab a single word, and it would not limit the amount of characters grabbed, which could lead to an overflow and data corruptions / program crashing.要回答您的第一个问题,对于数组,您应该使用cin.get而不是重载的运算符>> cin>>因为它只会抓取一个单词,并且不会限制抓取的字符数量,这可能会导致溢出和数据损坏/程序崩溃。

On the other hand, cin.get() allows you to specify the maximum number of characters read, preventing such bugs.另一方面, cin.get()允许您指定读取的最大字符数,从而防止此类错误。

For a char array use cin.get() because it counts whitespace whereas cin does not.对于 char 数组,请使用 cin.get() 因为它计算空格而 cin 不计算。 More importantly, cin.get() sets the maximum number of characters to read.更重要的是,cin.get() 设置要读取的最大字符数。 For example:例如:

 char foo[25]; //set maximum number of characters 
 cout << "Please type in characters for foo << endl;
 cin.get(foo,25);
 cout << ' ' << foo;

In this case, you can only type in 24 characters and a null terminator character '\\0'.在这种情况下,您只能输入 24 个字符和一个空终止符 '\\0'。
I hope this answers your first question.我希望这能回答你的第一个问题。 I would personally use a string.我个人会使用字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM