我有以下代碼:怪異的行爲與標準::函數getline和std ::矢量<std::string>
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;
}
鑑於輸入:
3
Here Goes
Here Goes 2
的輸出是:
<blank line>
Here Goes
Here Goes 2
奇怪的是,它第一次運行時似乎輸入一個空白行作爲輸入。
但是,如果我的代碼爲:
int tries = 3; // explicitly specifying the number of tries
int counter = 0;
while(counter < tries) {}
它將按預期工作。爲什麼std::cin >> tries
導致代碼失敗?
我與VC++ 2010和g ++ 4.4.3
謝謝,我現在明白了。 – user225312 2010-09-01 20:13:10