#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
//file();
vector<string> coll;
copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(coll));
sort(coll.begin(), coll.end());
unique_copy(coll.begin(), coll.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
如果从文件读入,可以读入字符串并终止程序,并且正常执行后面的程序,就是可以打印出读入的字符串。但是如果是从控制台读入,就无法终止了,不知道这个时候该怎可办才能够让程序停止读入并正常执行下去。如果直接ctrl+c,就是把整个程序都直接终止了。
ctrl z under Windows, ctrl d under *nix can send EOF to the program
You can set a condition to exit the loop. For example, if you read the string quit, you will exit the loop and continue execution. Librazy's answer is the correct answer.
If you need to implement Ctrl+C to terminate the loop and continue the program outside the loop, you can use signal. This is a set of mechanisms provided by the operating system. When you press Ctrl+C, a signal is actually sent to the process.