vector<string> ivec;
string str;
while(cin >> str)
ivec.push_back(str);
请问如何实现输入数据按回车结束,而不是crtl +z
PHP中文网2017-04-17 12:04:55
This question should not determine whether str is equal to 'n' or "n", but should determine whether str is empty, that is,
vector<string> ivec;
string str;
while(getline(cin, str))
if (str.empty()) break;
ivec.push_back(str);
Cause: When you don’t enter any characters and just hit Enter, getline only encounters a newline character, and getline extracts and discards it, so str is empty.