我练习编写了下面的一段小程序
#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就可以结束循环输入。