STL常用容器(7)--set/multiset容器

1 set/multiset容器基本概念

1.1 set容器基本概念

set的特性是。所有元素都会根据元素的键值自动被排序

set的元素不像map那样可以同时拥有实值和键值,set的元素即是键值又是实值。set不允许两个元素有相同的键值。

我们可以通过set的迭代器改变set元素的值吗?不行,因为set元素值就是其键值,关系到set元素的排序规则。如果任意改变set元素值,会严重破坏set组织。换句话说,set的iterator是一种const_iterator.

set拥有和list某些相同的性质,当对容器中的元素进行插入操作或者删除操作的时候,操作之前所有的迭代器,在操作完成之后依然有效,被删除的那个元素的迭代器必然是一个例外。


1.2 multiset容器基本概念

multiset特性及用法和set完全相同,唯一的差别在于它允许键值重复。set和multiset的底层实现是红黑树,红黑树为平衡二叉树的一种。



2 set常用API

2.1 set构造函数

set<T> st;				//set默认构造函数:
mulitset<T> mst; 		//multiset默认构造函数: 
set(const set &st);		//拷贝构造函数

2.2 set赋值操作

set& operator=(const set &st);	//重载等号操作符
swap(st);						//交换两个集合容器

2.3 set大小操作

size();		//返回容器中元素的数目
empty();	//判断容器是否为空

2.4 set插入和删除操作

insert(elem);		//在容器中插入元素。
clear();			//清除所有元素
erase(pos);			//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end);	//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem);		//删除容器中值为elem的元素。

2.5 set查找操作

find(key);				//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);				//查找键key的元素个数
lower_bound(keyElem);	//返回第一个key>=keyElem元素的迭代器。下限
upper_bound(keyElem);	//返回第一个key>keyElem元素的迭代器。上限
equal_range(keyElem);	//返回容器中key与keyElem相等的上下限的两个迭代器。相当与返回第一个key>=keyElem元素的迭代器 和 返回第一个key>keyElem元素的迭代器两个迭代器。


3 对组(pair)

对组(pair)将一对值组合成一个值,这一对值可以具有不同的数据类型,两个值可以分别用pair的两个公有属性first和second访问。

类模板:template <class T1, class T2> struct pair.

如何创建对组?

//第一种方法创建一个对组
pair<string, int> pair1("小红", 20);
cout << pair1.first << endl; //访问pair第一个值
cout << pair1.second << endl;//访问pair第二个值

//第二种
pair<string, int> pair2 = make_pair("小明", 30);
cout << pair2.first << endl;
cout << pair2.second << endl;

//pair=赋值
pair<string, int> pair3 = pair2;
cout << pair3.first << endl;
cout << pair3.second << endl;


4 案例

4.1 内置数据类型

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

// set容器的迭代器本身就是一个只读的迭代器
void printSet(const set<int> &S)
{
    for (set<int>::iterator it = S.begin(); it != S.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;
}

int main()
{
    set<int> S;

    S.insert(10);
    S.insert(20);
    S.insert(30);
    S.insert(40);

    if (S.empty())
    {
        cout << "set容器为空" << endl;
    }
    else
    {
        printSet(S);
        cout << " S.size() = " << S.size() << endl;
    }

    cout << "=====================" << endl;
    cout << "set容器查找操作" << endl;
    // set查找操作
    set<int>::iterator it = S.find(20);

    if (it != S.end())
    {
        cout << "找到了,值为:" << *it << endl;
    }
    else
    {
        cout << "没有找到" << endl;
    }

    int num = S.count(20);
    cout << "set容器中key为20的个数:" << num << endl;

    it = S.lower_bound(30); // 找>=31的值
    if (it != S.end())
    {
        cout << "找到lower_bound(30),值为:" << *it << endl;
    }
    else
    {
        cout << "没有找到lower_bound(30)" << endl;
    }

    it = S.upper_bound(30); // 找>30的值
    if (it != S.end())
    {
        cout << "找到upper_bound(30),值为:" << *it << endl;
    }
    else
    {
        cout << "没有找到upper_bound(30)" << endl;
    }

    pair<set<int>::iterator, set<int>::iterator> pit = S.equal_range(30);
    if (pit.first != S.end())
    {
        cout << "找到了equal_range(30)中第一个值(>=30的值):" << *pit.first << endl;
    }
    else
    {
        cout << "没有找到equal_range(30)中的第一个值(>=30的值)" << endl;
    }

    if (pit.second != S.end())
    {
        cout << "找到了equal_range(30)中第二个值(>30的值):" << *pit.second << endl;
    }
    else
    {
        cout << "没有找到equal_range(30)中的第二个值(>30的值)" << endl;
    }


    return 0;
}

在这里插入图片描述


4.2 使用对组pair判断set容器是否插入成功

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

int main()
{
    set<int> S;

	//使用对组pair判断set容器是否插入成功
    pair<set<int>::iterator, bool> p = S.insert(10);

    if (p.second)
    {   
        cout << "第一次插入成功" << endl;
    }   
    else
    {   
        cout << "第一次插入失败" << endl;
    }   

    p = S.insert(10);
    if (p.second)
    {   
        cout << "第二次插入成功" << endl;
    }   
    else
    {   
        cout << "第二次插入失败" << endl;
    }   

    for (auto it = S.begin(); it != S.end(); ++it)
    {   
        cout << *it << " ";
    }   
    cout << endl;

    return 0;
}

在这里插入图片描述

4.3 自定义数据类型

4.3.1 指定排序规则

对于自定义数据类型必须指定排序规则,否则无法构造set容器,因为set容器所有元素都会根据元素的键值自动被排序

指定排序规则的的方法:

  1. 方法一:在自定义数据类型中重载 " < " [bool operator<()]
  2. 方法二:写一个仿函数,指定排序规则,在构造set容器时使用
#include <iostream>
#include <set>
using namespace std;

class Person
{
public:
    Person(string name, int age, int hight)
    {   
        this->m_Name  = name;
        this->m_Age   = age;
        this->m_Hight = hight;
    }

#if 1
    // 方法一
    bool operator<(const Person& p)  const   // 重载<  注意要写成常函数
    {   
        if (this->m_Age == p.m_Age)     // 如果年龄相同,按照身高升序排列
        {   
            return this->m_Hight < p.m_Hight;
        }
        return this->m_Age < p.m_Age;   // 年龄不同,按照年龄升序排列
    }
#endif
    
    string m_Name;      // 姓名
    int    m_Age;       // 年龄
    int    m_Hight;     // 身高
};

#if 0
// 方法二
class myCompare
{
public:
    bool operator()(const Person &p1, const Person& p2) // 仿函数
    {
        if (p1.m_Age == p2.m_Age)
        {
            return p1.m_Hight < p2.m_Hight; 
        }
        return p1.m_Age < p2.m_Age;        
    }
};
#endif

int main()
{
#if 1
    // 方法一,重载<号时set容器构造方法
    set<Person> S;
#endif

#if 0
    // 方法二,使用仿函数时set容器构造方法
    set<Person, myCompare> S;
#endif

    Person p1("张三", 18, 170);
    Person p2("李四", 20, 170);
    Person p3("王五", 20, 172);
    Person p4("小明", 17, 173);
    Person p5("小红", 17, 173);

    S.insert(p1);
    S.insert(p2);
    S.insert(p3);
    S.insert(p4);
    S.insert(p5);                   // 插入失败, 排序规则是通过年龄和身高进行排序的,当年龄和身高相同时,尽管姓名不同,任然不能插入成功

    for (auto it = S.begin(); it != S.end(); ++it)
    {
        cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Hight << endl;
    }


#if 0
    // 方法二的迭代器为set<Person, myCompare>::iterator
    for (set<Person, myCompare>::iterator it = S.begin(); it != S.end(); ++it)
    {   
        cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << " 身高:" << it->m_Hight << endl;
    }   
#endif

    return 0;
}

在这里插入图片描述


4.3.2 将自定义数据类型通过位域组成内置数据类型

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

struct A
{
    // 通过位域将下面三个字段组成一个uint64_t类型的数据
    // 这样就不需要重载<号,或者使用仿函数指定排序规则了
    uint64_t b1:36;
    uint64_t b2:16;
    uint64_t b3:12;
};

int main()
{
    set<uint64_t> S;

    A a1; 
    a1.b1 = 12345;
    a1.b2 = 12; 
    a1.b3 = 10; 

    A a2; 
    a2.b1 = 1234;
    a2.b2 = 11; 
    a2.b3 = 15; 

    A a3; 
    a3.b1 = 123;
    a3.b2 = 0;
    a3.b3 = 0;

    S.insert(*(uint64_t*)&a1);
    S.insert(*(uint64_t*)&a2);
    S.insert(*(uint64_t*)&a3);

    for (set<uint64_t>::iterator it = S.begin(); it != S.end(); ++it)
    {   
        cout << *it << endl;
    }   

    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值