C++ cin.get() 和 cin.getline() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26873401/
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 20:41:19  来源:igfitidea点击:

difference between cin.get() and cin.getline()

c++getcingetline

提问by Yu Zhou

I am new to programming, and I have some questions on get()and getline()functions in C++.

我是编程新手,我对C++ 中的函数get()getline()函数有一些疑问。

My understanding for the two functions:

我对这两个功能的理解:

The getline()function reads a whole line, and using the newline character transmitted by the Enter key to mark the end of input. The get()function is much like getline()but rather than read and discard the newline character, get()leaves that character in the input queue.

getline()函数读取一整行,并使用回车键传输的换行符来标记输入的结束。该get()函数很像getline()但不是读取和丢弃换行符,而是get()将该字符留在输入队列中。

The book(C++ Primer Plus) that I am reading is suggesting using get()over getline(). My confusion is that isn't getline()safer than get()since it makes sure to end line with '\n'. On the other hand, get()will just hangs the character in the input queue, thus potentially causing problem?

我正在阅读的书(C++ Primer Plus)建议使用get()over getline(). 我的困惑是这并不getline()安全,get()因为它确保以'\n'. 另一方面,get()会不会只是将字符挂在输入队列中,从而可能导致问题?

回答by Emilio Garavaglia

There are an equivalent number of advantages and drawbacks, and -essentially- all depends on what you are reading: get()leaves the delimiter in the queue thus letting you able to consider it as part of the next input. getline()discards it, so the next input will be just after it.

有相同数量的优点和缺点,并且 - 基本上 - 都取决于您正在阅读的内容:get()将分隔符留在队列中,从而让您能够将其视为下一个输入的一部分。getline()丢弃它,所以下一个输入将在它之后。

If you are talking about the newline character from a console input,it makes perfectly sense to discard it, but if we consider an input from a file, you can use as "delimiter" the beginning of the next field.

如果您正在谈论来自控制台输入的换行符,丢弃它是完全有意义的,但是如果我们考虑来自文件的输入,您可以将下一个字段的开头用作“分隔符”。

What is "good" or "safe" to do, depends on what you are doing.

什么是“好”或“安全”,取决于你在做什么。

回答by Shubham Aggarwal

get()extracts char by char from a stream and returns its value (casted to an integer) whereas getline()is used to get a line from a file line by line. Normally getline is used to filter out delimiters in applications where you have a flat file(with thousands of line) and want to extract the output(line by line) using certain delimiter and then do some operation on it.

get()从流中逐个字符地提取字符并返回其值(转换为整数),而getline()用于逐行从文件中获取一行。通常 getline 用于在具有平面文件(包含数千行)并希望使用某些分隔符提取输出(逐行)然后对其进行一些操作的应用程序中过滤掉分隔符。

回答by congar

cin.getline()reads input up to '\n' and stops

cin.getline()读取输入到 '\n' 并停止

cin.get()reads input up to '\n' and keeps '\n' in the stream

cin.get()读取输入到 '\n' 并将 '\n' 保留在流中

For example :

例如 :

char str1[100];
char str2[100];
cin.getline(str1 , 100);
cin.get(str2 , 100);
cout << str1 << " "<<str2;

input :
1 2
3 4
output 1 2 3 4 // the output expexted

input :
1 2
3 4
output 1 2 3 4 // 输出扩展

When reverse them
For example :

当反转它们
例如:

char str1[100];
char str2[100];
cin.get(str2 , 100);
cin.getline(str1 , 100);
cout << str1 << " "<<str2;

input :
1 2
3 4
output 1 2 // the output unexpexted because cin.getline() read the '\n'

input :
1 2
3 4
output 1 2 // 输出未扩展,因为 cin.getline() 读取了 '\n'

回答by Omar Ashraf

cin.get() takes the input of whole line which includes end of line space repeating it will consume the next whole line but getline() is used to get a line from a file line by line.

cin.get() 获取整行的输入,其中包括行尾空间重复它会消耗下整行,但 getline() 用于逐行从文件中获取一行。