STL-set基础

Posted on 2018-04-24 21:04  som_nico  阅读(132)  评论(0编辑  收藏  举报

集合——set

也就是说set中没有重复元素

构造set集合的主要目的是为了快速检索

另,set是以不连续的节点形式存储的容器(list、set、map)

1.构建

#include<iostream>
#include<set>
using namespace std;
int main()
{
    set<int> s;
    return 0;
}

 

2.插入元素&&遍历(使用迭代器)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    set<int>s;
    for(int i = 0; i < 5; i++)
        s.insert(i);
    s.insert(0);//尝试多重复插入元素
    set<int>::iterator it;//定义正向迭代器
    for(it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
    cout << endl;
    set<int>::reverse_iterator rit; //定义反向迭代器
    for(rit = s.rbegin(); rit != s.rend(); rit++)
        cout << *rit << " ";
    cout << endl;
    return 0;
}

 3.删除

有关erase()函数的使用方法可以看看STL的erase()陷阱-迭代器失效总结这篇文章。

erase会删除这个节点,然后用后面补齐。

4.元素的查找

使用find()方法对集合进行检索,如果找到查找的的键值,则返回该键值的迭代器位置;否则,返回集合最后一个元素后面的一个位置,即end()。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    set<int>s;
    for(int i = 0; i < 5; i++)
        s.insert(i);
    set<int>::iterator it;//定义正向迭代器
    int x;
    while(cin >> x)
    {
        it = s.find(x);
        if(it != s.end()) cout << "can find" << endl;
        else cout << "not find" << endl;
    }
    return 0;
}

count()返回1表示存在,返回0表示不存在

#include<bits/stdc++.h>
using namespace std;
int main()
{
    set<int>s;
    for(int i = 0; i < 5; i++)
        s.insert(i);
    set<int>::iterator it;//定义正向迭代器
    int x;
    while(cin >> x)
    {
        if(s.count(x)) cout<<"can find"<<endl;
        else cout<<"cant find"<<endl;
    }
    return 0;
}

 5.自定义比较函数

 使用insert将元素插入到集合中去的时候,集合会根据设定的比较函数奖该元素放到该放的节点上去。在定义集合的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值从小到大的顺序插入元素。但在很多情况下,需要自己编写比较函数。

编写比较函数有两种方法。

(1)如果元素不是结构体,那么可以编写比较函数。下面的程序比较规则为按键值从大到小的顺序插入到集合中。

#include<iostream>
#include<set>
using namespace std;
struct mycomp
{ //自定义比较函数,重载“()”操作符
    bool operator() (const int &a, const int &b)
    {
        if(a != b)
            return a > b;
        else
            return a > b;
    }
};
int main()
{
    set<int, mycomp> s; //采用比较函数mycomp
    s.insert(5); //第一次插入5,可以插入
    s.insert(1);
    s.insert(6);
    s.insert(3);
    s.insert(5); //第二次插入5,重复元素,不会插入
    set<int,mycomp>::iterator it;
    for(it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
    cout << endl;
    return 0;
}
/*
运行结果:6 5 3 1  
*/

(2)如果元素是结构体,那么可以直接把比较函数写在结构体内。

#include<iostream>
#include<set>
#include<string>
using namespace std;
struct Info
{
    string name;
    double score;
    bool operator < (const Info &a) const // 重载“<”操作符,自定义排序规则
    {
        //按score由大到小排序。如果要由小到大排序,使用“>”即可。
        return a.score < score;
    }
};
int main()
{
    set<Info> s;
    Info info;

    //插入三个元素
    info.name = "Jack";
    info.score = 80;
    s.insert(info);
    info.name = "Tom";
    info.score = 99;
    s.insert(info);
    info.name = "Steaven";
    info.score = 60;
    s.insert(info);

    set<Info>::iterator it;
    for(it = s.begin(); it != s.end(); it++)
        cout << (*it).name << " : " << (*it).score << endl; 
    return 0;
}
/*
运行结果:
Tom : 99
Jack : 80
Steaven : 60
*/

6.Set常用函数:

c++ stl容器set成员函数:begin()--返回指向第一个元素的迭代器

c++ stl容器set成员函数:clear()--清除所有元素

c++ stl容器set成员函数:count()--返回某个值元素的个数

c++ stl容器set成员函数:empty()--如果集合为空,返回true

c++ stl容器set成员函数:end()--返回指向最后一个元素的迭代器

c++ stl容器set成员函数:equal_range()--返回集合中与给定值相等的上下限的两个迭代器

c++ stl容器set成员函数:erase()--删除集合中的元素

c++ stl容器set成员函数:find()--返回一个指向被查找到元素的迭代器

c++ stl容器set成员函数:get_allocator()--返回集合的分配器

c++ stl容器set成员函数:insert()--在集合中插入元素

c++ stl容器set成员函数:lower_bound()--返回指向大于(或等于)某值的第一个元素的迭代器

c++ stl容器set成员函数:key_comp()--返回一个用于元素间值比较的函数

c++ stl容器set成员函数:max_size()--返回集合能容纳的元素的最大限值

c++ stl容器set成员函数:rbegin()--返回指向集合中最后一个元素的反向迭代器

c++ stl容器set成员函数:rend()--返回指向集合中第一个元素的反向迭代器

c++ stl容器set成员函数:size()--集合中元素的数目

c++ stl容器set成员函数:swap()--交换两个集合变量

c++ stl容器set成员函数:upper_bound()--返回大于某个值元素的迭代器

c++ stl容器set成员函数:value_comp()--返回一个用于比较元素间的值的函数