我練習編寫了下面的一段小程序
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
int main()
{
string word;
vector<string> text;
while (cin >> word)
{
text.push_back(word);
}
for(vector<string>::size_type sx = 0; sx != text.size(); ++sx)
{
cout << text[sx] << endl;
}
return 0;
}
卻發現程序停在while循環中出不來。調試的過程中發現輸入流對象始終有效,即使如入換行符依舊是這樣。這讓我非常困惑。如何才能跳出while循環呢?通過在MSDN論壇上提問得知有兩種方法可以跳出while循環,一種是:回車 + Ctrl ^ Z + 回車;另一種是:兩次ctrl+z就可以結束循環輸入。