本题主要学习的就是set的用法。。也知道set中元素有着从小到大排好序的性质。。
代码,详情见注释
#include<iostream>
#include<cctype>
#include<set>
#include<string>
#include<sstream>
using namespace std;
set<string> dict;
int main ()
{
string s,buf;
while (cin >> s)
{
int i;
for(i=0;i<s.size();i++)
{
if(isalpha(s[i])) //判断是否为字母 类似的函数还有isupper islower isdigit
s[i]=tolower(s[i]); //将字母转化成小写 类似的还有toupper
else s[i]=' '; //此处将不是字母的都转化为空格 而stringstream会滤掉空格 最后只会剩下单词
}
stringstream ss(s);
//ss>>buf; 不能写成这样 因为有可能在单词前面有标点 而在上面被转化成了空格就无法读入单词,还是应该用while将流中的东西读干净~
while(ss >> buf)
dict.insert(buf); //插入到set中 set保证里面的元素不重复
}
set<string>::iterator it; //此处为STL中的迭代器。。 要记住用法.begin() .end()
for(it=dict.begin();it!=dict.end();it++)
cout << *it << endl;
return 0;
}