Weird behaviour with std::getline and std::vector<std::string>
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following code: std::vector<std::string> final_output; std::string input; int tries = 0; std::cin >> tries; int counter = 0; while(counter < tries) { std::getline(std::cin, input); final_output.push_back(input); ++counter; } Given the input: 3 Here Goes Here Goes 2 The output is: <blank line> Here Goes Here Goes 2 Weirdly, it seems to enter a blank line as input for the first time it runs. However, if I have the code as: int tries = 3; // explicitly specifying the number of tries int counter = 0; while(counter < tries) {} It works