UVA10815 - Andy's First Dictionary
# 问题描述
输入一个文本,将不同的单词按字典序从小到大排列,不区分大小写。
# 思路
学习了使用c++中STL的set容器,从而将每个单词看出一个整体进行存储,其中使用stringstream将一个文本中的单词可以逐个分开。
# 代码
此代码为《算法竞赛入门经典(第2版)》中的代码,由于简便清晰,因此加以借鉴学习。
#include <iostream>
#include <cstdio>
#include <sstream>
#include <set>
#include <string>
#include <cctype>
using namespace std;
int main()
{
set<string> dict;
set<string>::iterator it;
string s,buf;
while(cin>>s)
{
for(int i=0;i<s.length();i++)
if(isalpha(s[i]))
s[i]=tolower(s[i]);
else
s[i]=' ';
stringstream ss(s);
while(ss>>buf) dict.insert(buf);
}
for(it =dict.begin();it!=dict.end(); it++)
cout<<*it<<endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
编辑 (opens new window)
上次更新: 2022/05/18, 19:03:08