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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29630513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:12:51  来源:igfitidea点击:

Difference between cin and cin.get() for char array

c++arraysstringnamespacescin

提问by Ruben P

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()? And why should i use cin.get(); with no parameter after my char initialisation?

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

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.

我的第二个问题是:我的 c++ 老师教我每次使用 cin.get() 初始化字符,在每次初始化字符数组或 int 数组或只是 int 或其他之后,再次放置 cin.get(); 之后。这就是我最初想问的。

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.

那么,怎么了?请有人为每个案例解释!谢谢你。

回答by Mike Seymour

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).

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

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.

第二个cin.get(a,256);cin.get();读取整行,然后使用行尾字符,以便重复此操作将读取下一行。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.

字符串将增长以适应任何数量的输入。

回答by Nazara

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. Say you start entering your values like this:

问题很可能在于您稍后输入值的方式。在cin.get()每次初始化后有“抢”的是被每次按下输入一次就把流中的换行符。假设您开始输入这样的值:

2 

a b c d...

Assuming you have pressed enter after 2, the newline character was also put on the stream. 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.

假设您在 2 之后按下了 Enter,换行符也被放入流中。当你cin.get()在那之后调用时,它会抓取并丢弃换行符,允许你的其余代码正确获取输入。

To answer your first question, for an array, you should use cin.getinstead 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()允许您指定读取的最大字符数,防止此类错误。

回答by Michael Davis

For a char array use cin.get() because it counts whitespace whereas cin does not. More importantly, cin.get() sets the maximum number of characters to read. For example:

对于 char 数组,请使用 cin.get() 因为它计算空格而 cin 不计算。更重要的是,cin.get() 设置要读取的最大字符数。例如:

 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'.
I hope this answers your first question. I would personally use a string.

在这种情况下,您只能输入 24 个字符和一个空终止符 '\0'。
我希望这能回答你的第一个问题。我个人会使用字符串。