#include <iostream>
#include <string>
using namespace std;
int main()
{
string username;
cout<< "username" ;
cin >> username;
}
所以我很好奇这两个代码之间有什么区别,我听说这是同一件事,但如果是,那为什么要用两种方法呢?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string username;
cout << "username" ;
getline (cin,username) ;
}
最佳答案
区别在于std::getline
— 顾名思义 — 从给定的输入流(可能是 std::cin
)和 operator>>
中读取行读一个单词1。
即std::getline
读取直到找到换行符并且 operator>>
读取到一个空格(由 std::isspace
定义)并找到。两者都从流中删除各自的分隔符,但不将其放入输出缓冲区。
1。注意 >>
也可以阅读数字 — int
, short
, float
, char
等
关于c++ - getline 和 std::istream::operator>>() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005015/