C++ 理解getline()';在流开始时,在空行上使用scanf时的s行为
我正在从STDIN阅读以下内容C++ 理解getline()';在流开始时,在空行上使用scanf时的s行为,c++,input,scanf,getline,C++,Input,Scanf,Getline,我正在从STDIN阅读以下内容 2 hello world bye world 使用以下代码: scanf("%d", &n); while(getline(cin, str)) cout << "*" << str << endl; 当我使用scanf(“%d\n”,&n)将代码修改为完全读入第一行时,getline会忽略第一个空行: *hello world * *bye world 我的问题是为什么getline忽略第一个空行
2
hello world
bye world
使用以下代码:
scanf("%d", &n);
while(getline(cin, str))
cout << "*" << str << endl;
当我使用scanf(“%d\n”,&n)
将代码修改为完全读入第一行时,getline
会忽略第一个空行:
*hello world
*
*bye world
我的问题是为什么getline
忽略第一个空行
我的问题是为什么getline
忽略第一个空行
事实并非如此<代码>扫描正在跳过它。格式字符串中任何数量的连续空白(如\n
)都会导致它跳过输入中任何数量的连续空白(如两个换行符)
我的问题是为什么getline
忽略第一个空行
事实并非如此<代码>扫描正在跳过它。格式字符串中任何数量的连续空白(如\n
)都会导致它跳过输入中任何数量的连续空白(如两个换行符)
*hello world
*
*bye world