and*_*dre 3 c++ operators getline extraction
我有给定的输入:
local
127.0.0.1 localhost
other
next
Run Code Online (Sandbox Code Playgroud)
使用以下代码,输出是一个空白,我期望其他.输出是"输出:"
#include <iostream>
using namespace std;
int main() {
std::string ip, domain, header;
std::getline(cin, header);
cin >> ip >> domain;
std::getline(cin, header);
std::cout << "output: " << header;
}
Run Code Online (Sandbox Code Playgroud)
但是,我注意到cin >> ip >> domain;在调用之前提取两次()时会出现此问题std::getline.如果我有的话,代码就像我期望的那样工作cin >> ip.当我使用双提取(operator>>)时,为什么我会看到这个奇怪的结果std::getline?
流operator>>提取在它提取的数据之前的空白空间,而不是之后.这意味着它将"localhost"提取到domain,但在流中保留新行.getline()然后读取这个换行符.
| 归档时间: |
|
| 查看次数: |
209 次 |
| 最近记录: |