zoukankan      html  css  js  c++  java
  • [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
    ", *it);
            printf("
    ");
            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;
    }
    

      

  • 相关阅读:
    在Visual Studio中怎样快速添加代码段
    18个不常见的C#关键字,您使用过几个?
    C# 非常好用的组元Tuple
    C# List根据另一个List集合或数组排序
    Expression 核心操作符、表达式、操作方法
    如何避免频繁创建临时对象
    C# 23种设计模式
    C# 23种设计模式
    Api Cloud官方日期类型转换
    sql server 保留小数(续A)
  • 原文地址:https://www.cnblogs.com/TbIblog/p/11193641.html
Copyright © 2011-2022 走看看