输入一个文本,找出所有不同的单词(连续的字母序列),按字典序从小到大输出。单
词不区分大小写。
样例输入:
Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."
So they went home.
样例输出(为了节约篇幅只保留前
5
行):
a
adventures
blondes
came
disneyland
#include<iostream>
#include<string>
#include<set>
#include<sstream> //得到字符串里的每个单词
using namespace std;
set<string>dict;
main()
{ string s,buf;
while(cin>>s)
{ for(int i=0;i<s.length();i++)
if(isalpha(s[i])) //判断字符是否为英文字母(小写字母返回2,大写为1)
s[i]=tolower(s[i]); //把字母字符转换成小写,非字母不做处理
else s[i]=' ';
stringstream ss(s); //将s复制到stringstream.ss里面 (ss随意取名字)
while(ss>>buf) //把ss里面的单词一个个输出
dict.insert(buf); // 将单词插入里面
}
for(set<string>::iterator it=dict.begin();it!=dict.end();++it) //集合排序
cout<<*it<<"\n" //输出必须带上*
return 0;
}
#include<string>
#include<set>
#include<sstream> //得到字符串里的每个单词
using namespace std;
set<string>dict;
main()
{ string s,buf;
while(cin>>s)
{ for(int i=0;i<s.length();i++)
if(isalpha(s[i])) //判断字符是否为英文字母(小写字母返回2,大写为1)
s[i]=tolower(s[i]); //把字母字符转换成小写,非字母不做处理
else s[i]=' ';
stringstream ss(s); //将s复制到stringstream.ss里面 (ss随意取名字)
while(ss>>buf) //把ss里面的单词一个个输出
dict.insert(buf); // 将单词插入里面
}
for(set<string>::iterator it=dict.begin();it!=dict.end();++it) //集合排序
cout<<*it<<"\n" //输出必须带上*
return 0;
}
记事本
set,集合,每个元素最多出现一次
s.insert (x) 把元素插入set集合里面
set<int>::iterator it; //集合排序
for(it=s.begin ();it!=s.end ();it++)
{
printf("%d\n",*it);
}
//s.end()没有值
cout<<"s.begain() "<<*s.begin ()<<endl;
//lower_bound()--返回指向大于(或等于)某值的第一个元素的迭代器
cout<<"lower_buond 3 "<<*s.lower_bound (3)<<endl;
//upper_bound()--返回大于某个值元素的迭代器
cout<<"upper_bound 3 "<<*s.upper_bound (3)<<endl;
//find()--返回一个指向被查找到元素的迭代器
cout<<"find() 3 "<<*s.find (3)<<endl;
cout<<"s.size() "<<s.size ()<<endl;
isalpha(a[i]) //判断字符是否为英文字母(小写字母返回2,大写为1)
tolower(a[i]) //把字母字符转换成小写,非字母不做处理