STL:set

常用操作



#include<set>

using namespace std;
set<int>:s;

1. 元素插入:s.insert()


2. 中序遍历:类似vector遍历(用迭代器)
set<int>::reverse_iterator rit = s.begin();
while(  rit != s.end()  )
{
  cout << *rit << endl;
  rit ++;
}


3. 反向遍历:利用反向迭代器 reverse_iterator。

set<int>::reverse_iterator rit;
for(  rit = s.rbegin() ;  rit != s.rend() ;  rit++  )


4. 元素删除:与插入一样,可以高效地删除,并自动调整使红黑树平衡。
set<int> s;
ss.erase(2);      // 删除键值为2的元素
ss.clear();


5. 元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。
set<int> ss;
set<int>::iterator it;   

 // 迭代器的类型必须与查找元素的容器类型相同,否则会出错
it = s.find(5);      // 查找键值为5的元素
if( it != s.end() )     // 找到
  cout << *it << endl;
else            // 未找到
  cout << "未找到";


成员函数




c++ stl容器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()   -- 返回一个用于比较元素间的值的函数


补充



/***对于 set 的补充

   如果我们在存进set的时候不是单纯的存一个类型
   而是一个结构体呢???那就出现问题了

   因此想要他按照我们的意愿来排序的话就需要对'<'进行重载了
**/
typedef struct AA
{
   int val,b;
   bool operator < ( const AA & CC ) const
   {
       return val<CC.val;
   }
}my_type;

///然后你就可以用my_type当作一个类型来使用了,注意他是以 .val 进行排序的



例题:安迪的第一个字典(Andy's First Dictionary, Uva 10815)



        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. Furthermore, your program must be CaSe InSeNsItIvE. 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. Input is terminated by EOF.
Output
Your output should give a list of different words that appears in the input text, one in a line. The words 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.
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

adventures

blondes

came

disneyland

fork

going

home

in

left

read

road

sign

so

the

they

to

two

went

were

when


code


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

set<string> dict;

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



最后可以搜一下set_union和set_intersection

求并集交集的

由于比较复杂 懒得放上来了


猜你喜欢

转载自blog.csdn.net/qq_42390424/article/details/80855064