[STL] UVA 10815 安迪的第一个字典 Andy's First Dictionary

1、set

集合 哦....对了,set有自动按照字典序排序功能。。。。。

声明和插入操作

#include <cstdio>
#include <vector>
#include <set>
using namespace std;
int main(){
        vector<int> v;
        for (int i = 0; i < 10; i++)
        {
                v.push_back(i);
                v.push_back(i);
        }
        set<int> s;
        s.insert(v.begin(), v.end());
        //因为是集合,所以重复的元素不会被重复插入
        for (set<int>::iterator it = s.begin(); it != s.end(); it++)
                printf("%d\n", *it);
        printf("\n");
        return 0;
}

 

删除操作

这个一般都用的是erase()--->删除指定的元素或者删除指定部分的元素 和 clear()--->清除所有元素 操作如下:

    set<int> s;
    s.erase(2);        
    s.clear();

 

查找操作

set支持upper_bound() lower_bound() find()等操作 举个例子:

   set<int> s;
   set<int>::iterator it;
   it = s.find(5);    //查找键值为5的元素
   if (it != s.end())    //找到了 
   cout << *it << endl;

 

2、isalpha()

判断字符ch是否为英文字母,若为英文字母,返回非0(小写字母为2,大写字母为1)。若不是字母,返回0。 具体用法见下

3、stringstream()

输入输出转换操作 具体用法见下

4、tolower()

是一种函数,功能是把字母字符转换成小写,非字母字符不做出处理。 具体用法见下

下面是该题代码:

  

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<set>
using namespace std;
set<string>aa;
string s,cur;
int main(){
    std::ios::sync_with_stdio(false);
    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>>cur)  
            aa.insert(cur);
    }
    for(set<string>::iterator it=aa.begin();it!=aa.end();it++)
    {
        cout<<*it<<endl;
    }
    return 0;
}

  

posted @ 2019-07-16 11:13  ты  阅读(165)  评论(0编辑  收藏  举报