标签:
问题链接:UVA10815 Andy‘s First Dictionary。
题意简述:输入一个文本文件,从中提取出字典,重复的单词被去掉。
这个问题用C++语言编写程序,主要是为了练习使用STL的功能。另外一点,C++编写程序效率会更高。
使用STL容器类的set,可以方便地去重复,而且还会自动排序。
程序中,使用C语言的库函数strtok()来切割单词,并且用空格‘ ‘作为分隔符。这是一种简便的做法。
另外一种切割字符串的方法是,使用STL的字符串流(sstream)实现。
AC的C++程序如下:
/* UVA10815 Andy's First Dictionary */
#include <iostream>
#include <cstring>
#include <set>
using namespace std;
#define MAXN 512
set<string> dict;
int main()
{
char s[MAXN], delim[] = " ", *p;
while(cin >> s) {
p = s;
while(*p) {
if(isalpha(*p))
*p = tolower(*p);
else
*p= ' ';
p++;
}
p = strtok(s, delim);
while(p) {
dict.insert(p);
p = strtok(NULL, delim);
}
}
for(set<string>::iterator iter =dict.begin(); iter != dict.end(); iter++)
cout << *iter << "\n";
return 0;
}UVA10815 Andy's First Dictionary
标签:
原文地址:http://blog.csdn.net/tigerisland45/article/details/52098242