题意:
输入一段文本,找出所有不同的单词,并按字典序由小到大输出,单词不区分大小写。
思路:
输入时将所有的非字母元素转化为空格,在进行字符串的解析即可。stringstream在解析字符串是以空格或回车作为结束标志。
#include <set>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
set <string> word;
set <string>::iterator iter;
int main(){
// freopen("in.txt", "r", stdin);
string s,buf;
while(cin >> s){
for(int i=0; i<s.length(); i++){
if(isalpha(s[i])) s[i] = tolower(s[i]);///isalpha()判断字符是否是字母,tolower()将字母转换为小写
else s[i] = ' ';
}
stringstream ss(s);///解析字符串
while(ss >> buf) word.insert(buf);///将解析结果ss存入buf中放入集合排序
}
for(iter = word.begin(); iter != word.end(); iter++)///遍历集合
cout << *iter << endl;
return 0;
}