UVA 10815 Andy's First Dictionary(stl,set)

来源:互联网 发布:饥荒mac中文版mod 编辑:程序博客网 时间:2024/05/13 01:18

UVA 10815 Andy’s First Dictionary
题目:给出一串单词,把所有单词改小写去重按字典序输出。
思路:set可以解决去重和排序问题。(这点很关键,从小到大排序)
set中每个元素最多只出现一次
如何通过迭代器从小到大遍历所有元素
for (set::iterator i = d.begin(); i != d.end(); i++)
cout << *i << endl;

#include <iostream>#include <string>#include <set>using namespace std;set<string>jihe;int main(){    string s;    while(getline(cin,s))    {        string tmp;        for(int i=0;i<=s.size();i++)        {            if(s[i]>='A'&&s[i]<='Z')            {                s[i]=s[i]+32;                tmp+=s[i];            }            else if(s[i]>='a'&&s[i]<='z')            {                tmp+=s[i];            }            else            {             if(!tmp.empty())                {                    jihe.insert(tmp);                    tmp.clear();                }            }        }        if(!tmp.empty())            jihe.insert(tmp);    }        for(set<string>::iterator it=jihe.begin();it!=jihe.end();it++)        {            cout<<*it<<endl;        }    return 0;}/*Adventures in DisneylandTwo blondes were going to Disneyland when they came to a fork in theroad. The sign read: "Disneyland Left."So they went home.*/
0 0
原创粉丝点击