您的位置:首页 > 编程语言 > C语言/C++

关于C++中输入操作符(>>)返回输入流对象的的问题

2013-08-14 11:10 288 查看
我练习编写了下面的一段小程序

#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就可以结束循环输入。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: