UVa-10815-安迪的第一个字典

这题主要是使用STL和c++解决的,我们使用set和字符流节省我们的工作量。
并且set中的元素不仅不重复,而且还按升序排序,这样写入set的直接输出就可以了。
我们读入一行之后,对于是不是字母进行判断,如果是的话,我们就把它转成小写写入,按题目要求;如果不是的话,我们就把它转成空格,这样我们用字符串s初始化ss之后,再按照字符流写入,就可以很方便地只写入单词了。

#include <iostream>
#include <string>
#include <set>
#include <sstream>
using namespace std;

set<string> dict;

int main()
{
    string buf, s;
    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);
    }
    set<string>::iterator it = dict.begin();
    for (; it != dict.end(); it++) {
        cout << *it << endl;
    }
    return 0;
}
posted @ 2019-02-02 17:29  xyee  阅读(97)  评论(0编辑  收藏  举报