入门经典第五章例题5-3

利用stringstream进行分词,把输入的字符串标准化后构建stringstream,以cin的方法从字符串流中读出来再添加到字典中。

using namespace std;
set<string> dict;
int main() {
	string s, buf;
	while (cin >> s) {
		for (int i = 0, len = s.length(); i < len; ++i) {
			if (isalpha(s[i])) s[i] = tolower(s[i]);
			else s[i] = ' ';
		}
		stringstream ss(s);
		while (ss >> buf) dict.insert(buf);
	}
	for (set<string>::iterator it = dict.begin(); it != dict.end(); ++it) 
		cout << *it << endl;
	return 0;
}
posted @ 2017-07-27 15:52  積水成淵  阅读(99)  评论(0编辑  收藏  举报