C++ std::cin.getline() 对比 std::cin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4745858/
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
std::cin.getline( ) vs. std::cin
提问by Simplicity
When should std::cin.getline()
be used? What does it differ from std::cin
?
应该std::cin.getline()
什么时候使用?它与什么不同std::cin
?
采纳答案by alvelcom
In case with char*, std::cin.getline
getting line, instead of std::cin
getting first word.
在 char* 的情况下,std::cin.getline
获取行,而不是std::cin
获取第一个单词。
回答by MSalters
Let's take std::cin.getline()
apart. First, there's std::
. This is the namespace in which the standard library lives. It has hundreds of types, functions and objects.
我们std::cin.getline()
拆开看看。首先,有std::
. 这是标准库所在的命名空间。它有数百种类型、函数和对象。
std::cin
is such an object. It's the standard character input object, defined in <iostream>
. It has some methods of its own, but you can also use it with many free functions. Most of these methods and functions are ways to get one or more characters from the standard input.
std::cin
是这样一个对象。它是标准字符输入对象,定义在<iostream>
. 它有自己的一些方法,但您也可以将它与许多免费功能一起使用。大多数这些方法和函数是从标准输入中获取一个或多个字符的方法。
Finally, .getline()
is one such method of std::cin
(and other similar objects). You tell it how many characters it should get from the object on its left side (std::cin
here), and where to put those characters. The precise number of characters can vary: .getline()
will stop in three cases:
1. The end of a line is reached
2. There are no characters left in the input (doesn't happen normally on std::cin
as you can keep typing)
3. The maximum number of characters is read.
最后,.getline()
是std::cin
(和其他类似对象)的一种这样的方法。您告诉它应该从其左侧的对象(std::cin
此处)获取多少个字符,以及将这些字符放在哪里。准确的字符数可能会有所不同:.getline()
将在三种情况下停止: 1. 到达行尾 2. 输入中没有剩余字符(正常情况下不会发生,std::cin
因为您可以继续输入) 3. 最大读取的字符数。
There are other methods and functions that can be used with the std::cin
object, e.g.
还有其他方法和函数可以与std::cin
对象一起使用,例如
std::string s;
int i;
std::cin >> s; // Read a single word from std::cin
std::cin >> i; // Read a single number from std::cin
std::getline(std::cin, s); // Read an entire line (up to \n) from std::cin
std::cin.ignore(100); // Ignore the next 100 characters of std::cin
回答by Oliver Charlesworth
Did you read any documentation (e.g. http://www.cplusplus.com/reference/string/getline/)?
您是否阅读过任何文档(例如http://www.cplusplus.com/reference/string/getline/)?
Basically, std::cin
(or more generally, any std::istream
) is used directly in order to obtain formatted input, e.g. int x; std::cin >> x;
. std::cin.getline()
is used simply to fill a raw char *
buffer.
基本上,std::cin
(或更一般地, any std::istream
)直接用于获取格式化输入,例如int x; std::cin >> x;
. std::cin.getline()
仅用于填充原始char *
缓冲区。
回答by Francesco Boi
As already others have answered (even better) roughly speaking, use getline()
to read an entire line (i.e., a string terminating with \n
) and cin>>var
to read a number compatible with the type of var
(integer, float, double etc.) or a single word.
正如其他人已经回答(甚至更好)粗略地说,用于getline()
读取整行(即以 结尾的字符串\n
)并cin>>var
读取与类型var
(整数、浮点数、双精度等)或单个单词兼容的数字。
In this answer I want to emphasize a problem that arises when mixing the two methods. When you do:
在这个答案中,我想强调混合这两种方法时出现的一个问题。当你这样做时:
int a;
string s;
cin>>a;
getline(cin, s)
cin
leaves an end of line, \n
, character which is then read by getline();
. It is possible to overcome this problem by using cin.ignore()
.
cin
留下行尾, \n
, 字符,然后由 读取getline();
。可以通过使用来克服这个问题cin.ignore()
。
int a;
string s;
cin>>a;
cin.ignore();
getline(cin, s)
回答by B0rKII
(Very simplefied)My answer is, that std :: cin.getline()
can contain spaces, while std :: cin >>
can not.
(非常简单)我的答案是,std :: cin.getline()
可以包含空格,而std :: cin >>
不能。