UVa10615 Andy's First Dictionary(集合set)

这道题主要用到了set容器和stringstream,用起来非常方便,我第一次见识到,觉得十分的炫酷……

而且,竟然可以将自己写的单词按照字典序排列,真的太酷了。

下面是书上的代码,目前还处于初学状态,所以都是摘抄例题的代码,希望不久之后,我可以用学到的技能自己做出题来。

晚安啦。

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

set<string> dict;//string集合 

int main(){
    string s,buf;
    while(cin>>s&&s!="quit"){
        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(set<string>::iterator it=dict.begin();it !=dict.end();++it)
        cout<<*it<<"\n";
    return 0;
}

 

posted @ 2017-02-09 23:01  wtzhang  阅读(200)  评论(0编辑  收藏  举报