《STL》set集合(C++)

SET

特点:

元素独一无二,有序,插入效率高。

基本函数:

begin()–返回指向第一个元素的迭代器

clear()–清除所有元素

count()–返回某个值元素的个数

empty()–如果集合为空,返回true

end()–返回指向最后一个元素的迭代器

equal_range()–返回集合中与给定值相等的上下限的两个迭代器

erase()–删除集合中的元素

find()–返回一个指向被查找到元素的迭代器

get_allocator()–返回集合的分配器

insert()–在集合中插入元素

lower_bound()–返回指向大于(或等于)某值的第一个元素的迭代器

key_comp()–返回一个用于元素间值比较的函数

max_size()–返回集合能容纳的元素的最大限值

rbegin()–返回指向集合中最后一个元素的反向迭代器

rend()–返回指向集合中第一个元素的反向迭代器

size()–集合中元素的数目

swap()–交换两个集合变量

upper_bound()–返回大于某个值元素的迭代器

value_comp()–返回一个用于比较元素间的值的函数

比较:

(1)如果元素是结构体,可以直接将比较函数写在结构体内。
(2)元素不是结构体,自定义比较函数。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<set>
#include<cctype>
#include<sstream>

using namespace std;

struct example1{
    
    //结构体重载‘<’,使本身有序。
    int x;
    int y;
    bool operator < (const example1 &a)const
    {
    
    
        return a.x<x;//从大到小排列
    }
};

struct example2{
    
    
    int x;
    int y;
};

struct cmp//自定义比较函数myComp,重载“()”操作符
{
    
    
    bool operator()(const example2 &a,const example2 &b)
    {
    
    
        return a.x-b.x>0;//从大到小排列
    }
};


int main()
{
    
    
  set<example1>set1;
  example1 ex1[5]={
    
    {
    
    5,1},{
    
    3,1},{
    
    4,1},{
    
    1,1},{
    
    2,1}};
  for(int i=0;i<5;i++)
    set1.insert(ex1[i]);

  set<example1>::iterator it;
  cout<<"自排序结构体:"<<endl;
  for(it=set1.begin();it!=set1.end();it++)
    cout<<(*it).x<<","<<(*it).y<<" ";
  cout<<endl;

  set<example2,cmp>set2;
  example2 ex2[5]={
    
    {
    
    5,1},{
    
    6,1},{
    
    4,1},{
    
    1,1},{
    
    2,1}};
  for(int i=0;i<5;i++)
    set2.insert(ex2[i]);

  set<example2,cmp>::iterator it2;
  cout<<"自定义比较结构体,重载():"<<endl;
  for(it2=set2.begin();it2!=set2.end();it2++)
    cout<<(*it2).x<<","<<(*it2).y<<" ";
  cout<<endl;
  return 0;
}

输出:

在这里插入图片描述

例题:Andy’s First Dictionary UVA - 10815

Description

Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is,well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case.Words with only one letter are also to be considered. Further more, your program must be case insensltive.For example, words like “Apple”, “apple” or “APPLE” must be considered the same.

Input

The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Inputis terminated by EOF.

输入文件是不超过5000行的文本。输入行最多包含200个字符。EOF终止输入。

Output

Your output should give a list of different words that appears in the input text, one in a line. Thewords should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.

输出请给出输入文本中出现的不同单词的列表,一个单词在一行中。这些字都应该是小写的,按字母顺序排列。文件中的不同单词的数量不超过5000。

Sample Input

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the

road. The sign read: “Disneyland Left.”

So they went home.

Sample Output

a

adventuresblondes

came

disneyland

fork

going

home

in

left

read

road

sign

so

the

they

to

two

went

were

when

AC代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<set>
#include<cctype>
#include<sstream>

using namespace std;


int main()
{
    
    
    ios::sync_with_stdio(false);

    string s,buf;
    set<string>dict;

    while(cin>>s)
    {
    
    
        for(int i=0;i<s.length();i++)
        {
    
    
            if(!isalpha(s[i])) s[i]=' ';//把非字母字符转化空格
            else s[i]=tolower(s[i]);//其它字母转化为小写字母
        }
        stringstream ss(s);
   //利用stringstream将带有空格的字符串的拆分成一个个字符,类似于java的split()
        while(ss>>buf) dict.insert(buf);
    }

    set<string>::iterator it;
    for(it=dict.begin();it!=dict.end();it++)
        cout<<*it<<endl;
    return 0;
}


注意的是:#include中有很多好用的函数,类似isalpha()//是不是字母,tolower(),
toupper(),等具体可参考:cctype头文件
其次就是#include 输入输出流:用其实现了空格的去除,其还可以实现c++中各种数据类型间的转换。具体可参考:sstream头文件

猜你喜欢

转载自blog.csdn.net/Look_star/article/details/107123657