【STL】关联式容器:set 与 map

1. set

1.1 set容器 简介

① set容器中所有元素在插入时自动被排序。

② set容器和multiset容器属于关联式容器,底层结构用红黑树实现。

③ set容器与multiset容器区别:

  1. set容器不允许容器中有重复的元素。
  2. multiset容器允许容器中有重复的元素。

1.2 set常用操作函数

1. set<T> st;  //默认构造函数
2. set(const set &st);  //拷贝构造函数
3. set& operator=(const set &st);  //重载等号操作符
4. size();   //返回容器中元素的数目。
5. empty();  //判断容器是否为空。
6. swap(st);  //交换两个集合容器
7. insert(elem);  //在容器中插入元素。
8. clear();  //清除所有元素。
9. erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
10. erase(beg,end); //删除区间[beg,end)的所有元素,返回下一个元素的迭代器。
11. erase(elem); //删除容器中值为elem的元素。
12. find(key);  //查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回set.end();
13. cout(key);   //统计key的元素个数。

1.3 构造和赋值

① 功能描述:创建set容器以及赋值。
② 构造函数:

1. set<T> st;  //默认构造函数
2. set(const set &st);  //拷贝构造函数

③ 赋值函数:

3. set& operator=(const set &st);  //重载等号操作符

④ set容器插入数据时用insert。
⑤ set容器插入的数据会自动排序。

#include <iostream>
#include <set>

using namespace std;

//set容器  构造和赋值

void printset(const set<int>&L)
{
    for (set<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

bool myCopare(int v1, int v2)
{
    //降序 就让第一个数 大于第二个数为真
    return v1 > v2;

}

void test01()
{
    set<int>s1;  

    //插入数据  只有insert方式
    s1.insert(10);
    s1.insert(40);
    s1.insert(30);
    s1.insert(20);
    s1.insert(30);

    //遍历容器
    //set容器特点:所有元素插入时候自动被排序
    //set容器不允许插入重复值
    printset(s1);

    //拷贝构造
    set<int>s2(s1);
    printset(s2);

    //赋值
    set<int>s3;
    s3 = s2;
    printset(s3);
}


int main()
 {
    test01();

    return 0;
}

运行结果:
在这里插入图片描述

1.4 大小和交换

① 功能描述:统计set容器大小以及交换set容器。
② 函数原型:

1. size();   //返回容器中元素的数目。
2. empty();  //判断容器是否为空。
3. swap(st);  //交换两个集合容器
#include <iostream>
#include <set>

using namespace std;


//set容器  大小和交换

void printset(const set<int>&L)
{
    for (set<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}


//大小
void test01()
{
    set<int>s1;  

    //插入数据  只有insert方式
    s1.insert(10);
    s1.insert(40);
    s1.insert(30);
    s1.insert(20);
    s1.insert(30);

    printset(s1);

    //判断是否为空
    if (s1.empty())
    {
        cout << "s1为空" << endl;
    }
    else
    {
        cout << "s1不为空" << endl;
        cout << "s1的大小为:" << s1.size() << endl;
    }
}

//交换
void test02()
{
    set<int>s1;

    //插入数据  只有insert方式
    s1.insert(10);
    s1.insert(40);
    s1.insert(30);
    s1.insert(20);
    s1.insert(30);

    set<int>s2;

    //插入数据  只有insert方式
    s2.insert(100);
    s2.insert(400);
    s2.insert(300);
    s2.insert(200);
    s2.insert(300);

    cout << "交换前:" << endl;
    printset(s1);
    printset(s2);

    cout << "交换后:" << endl;
    s1.swap(s2);
    printset(s1);
    printset(s2);
}

int main()
 {
    test01();
    test02();

    return 0;
}

运行结果:
在这里插入图片描述

1.5 插入和删除

① 功能描述:set容器进行插入数据和删除数据。
② 函数原型:

1. insert(elem);  //在容器中插入元素。
2. clear();  //清除所有元素。
3. erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器。
4. erase(beg,end); //删除区间[beg,end)的所有元素,返回下一个元素的迭代器。
5. erase(elem); //删除容器中值为elem的元素。
#include <iostream>
#include <set>

using namespace std;

//set容器  插入和删除

void printset(const set<int>&L)
{
    for (set<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test01()
{
    set<int>s1;  

    //插入数据  只有insert方式
    s1.insert(20);
    s1.insert(40);
    s1.insert(30);
    s1.insert(10);
    s1.insert(30);

    printset(s1);

    //删除
    s1.erase(s1.begin());  //删掉的是排序后的第一个元素10
    printset(s1);

    //删除函数的重载版本
    s1.erase(30);   //删除30这个元素
    printset(s1);

    //清空方式一:
    s1.erase(s1.begin(), s1.end());
    printset(s1);

    //清空方式二:
    s1.clear();
    printset(s1);
}

int main() 
{
    test01();

    return 0;
}

运行结果:
在这里插入图片描述

1.6 查找和统计

① 功能描述:对set容器进行查找数据以及统计数据。
② 函数原型:

1. find(key);  //查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回set.end();
2. cout(key);   //统计key的元素个数。
#include <iostream>
#include <set>

using namespace std;

//set容器  查找和统计

void printset(const set<int>&L)
{
    for (set<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}


void test01()
{
    set<int>s1;  

    //插入数据  只有insert方式
    s1.insert(20);
    s1.insert(40);
    s1.insert(30);
    s1.insert(10);
    s1.insert(30);

    printset(s1);

    //查找返回的是一个迭代器
    set<int>::iterator pos = s1.find(30);   
    if (pos != s1.end())
    {
        cout << "找到元素:" << *pos << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }
}

//统计
void test02()
{
    set<int>s1;

    //插入数据  只有insert方式
    s1.insert(20);
    s1.insert(40);
    s1.insert(30);
    s1.insert(10);
    s1.insert(30);

    int num = s1.count(30);
    //对于set而言 统计结果要么是0 要么是1
    cout << "num = " << num << endl;
}

int main()
 {
    test01();
    test02();

    return 0;
}

运行结果:
在这里插入图片描述

1.7 set和multiset区别

① set和multiset区别:

  1. set不可以插入重复数据,而multiset可以。
  2. set插入数据的同时会返回插入结果,表示插入是否成功。
  3. mutiset不会检测数据,因此可以插入重复数据。
    ② 如果不允许插入重复数据可以利用set
    ③ 如果需要插入重复数据利用mutiset
#include <iostream>
#include <set>

using namespace std;

//set容器 和 mutiset容器 的区别

void printset(const set<int>&L)
{
    for (set<int>::const_iterator it = L.begin(); it != L.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void test01()
{
    set<int>s1;  

    pair<set<int>::iterator, bool> ret = s1.insert(10);  //s.insert(数)返回的是pair类型,第一个数为迭代器,第二个数为布尔类型
    if (ret.second)
    {
        cout << "第一次插入成功" << endl;
    }
    else
    {
        cout << "第一次插入失败" << endl;
    }
    
    ret = s1.insert(10);  //set不允许插入重复的值,当插入重复的值,返回的第二个参数为False,然后不会插入进去

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

    multiset<int>ms;
    //运行插入重复值
    ms.insert(10);
    ms.insert(10);
    ms.insert(10);

    for (multiset<int>::const_iterator it = ms.begin(); it != ms.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

int main() 
{
    test01();


    return 0;
}

运行结果:
在这里插入图片描述

1.8 内置类型指定排序规则

① set容器默认排序规则从小到大,利用仿函数,可以改变排序规则。

#include <iostream>
#include <set>

using namespace std;


//set容器排序

class MyCompare
{
public:
    bool operator()(int v1, int v2)const //第一个()表示重载符号,第二个()为参数列表
    {
        return v1 > v2;
    }
};

void test01()
{
    set<int>s1;  //set容器默认从小到大排序

    s1.insert(10);
    s1.insert(40);
    s1.insert(50);
    s1.insert(20);
    s1.insert(30);

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

    //指定排序规则为从大到小

    set<int, MyCompare>s2;  //此时指定set容器的排序规则为MyCompare,MyCompare()

    s2.insert(10);
    s2.insert(40);
    s2.insert(50);
    s2.insert(20);
    s2.insert(30);

    for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

int main()
{
    test01();
    
    return 0;
}

运行结果:
在这里插入图片描述

1.9 自定义数据类型指定排序规则

#include <iostream>
#include <set>

using namespace std;

//set容器排序

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

class comparePerson //仿函数本质是一个类
{
public:
    bool operator()(const Person& p1, const Person& p2)const
    {
        //安装年龄 降序
        return p1.m_Age > p2.m_Age;
    }
};

void test01()
{
    //自定义数据类型  都会指定排序规则
    set<Person, comparePerson>s1;

    //创建Person对象
    Person p1("刘备", 24);
    Person p2("关羽", 28);
    Person p3("张飞", 25);
    Person p4("赵云", 21);

    s1.insert(p1);
    s1.insert(p2);
    s1.insert(p3);
    s1.insert(p4);

    for (set<Person, comparePerson>::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << "姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;
    }
}

int main()
{
    test01();

    return 0;
}

运行结果:
在这里插入图片描述

2. map

2.1 map容器 简介

① map中的所有元素都是pair。

② pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)。

③ 所有元素都会根据元素的键值自动排序。

④ map容器和multimap容器属于关联式容器,底层结构是用红黑树实现。

⑤ map容器可以根据key值快速找到value值。

⑥ map和multimap区别:

  1. map不允许容器中有重复key值元素。
  2. mutimap允许容器中有重复的key值元素。

2.2 map常用操作接口

  1. map<T1,T2> mp; //map默认构造函数
  2. map(const map &mp); //拷贝构造函数

2.3 map容器构造和赋值

① 功能描述:对map容器进行构造和赋值操作。
② 构造函数:

1. map<T1,T2> mp; //map默认构造函数
2. map(const map &mp);  //拷贝构造函数
3. size(); //返回容器中元素的数目。
4. empty();  //判断容器是否为空。
5. swap(st);  //交换两个集合容器。
6. 1. insert(elem); //在容器中插入元素。
7. clear();  //清除所有元素。
8. erase(pos);  //删除pos迭代器所指的元素,返回下一个元素的迭代器。
9. erase(beg,end); //删除区间[beg,end)的所有元素,返回下一个元素的迭代器。
10. erase(key);  //删除容器中值为key的元素。
11. find(key);  //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
12. cout(key); //统计key的元素个数。 

③ 赋值操作:

3. map& operator=(const map &mp);  //重载等号操作符

④ map容器中所有元素都是成对出现,插入元素时候需要使用对组。

#include <iostream>
#include <map>

using namespace std;

//map容器 构造和赋值

void printMap(map<int, int>& m)
{
    for (map<int,int>::iterator it = m.begin();it!=m.end();it++)
    {
        cout << "key = " << it->first << " value = " << it->second << endl;
    }
    cout << endl;
}

void test01()
{
    //创建map容器
    map<int, int>m;

    m.insert(pair<int, int>(1, 10));  //1为key;10为value
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(4, 40));

    printMap(m);

    //拷贝构造
    map<int, int>m2(m);
    printMap(m2);

    //赋值
    map<int, int>m3;
    m3 = m2;
    printMap(m3);
}

int main() 
{
    test01();

    return 0;
}

运行结果:
在这里插入图片描述

2.4 map容器大小和交换

① 功能描述:统计map容器大小以及交换map容器。
② 函数原型:

1. size(); //返回容器中元素的数目。
2. empty();  //判断容器是否为空。
3. swap(st);  //交换两个集合容器。
#include <iostream>
#include <map>

using namespace std;

void printMap(map<int, int>& m)
{
    for (map<int,int>::iterator it = m.begin();it!=m.end();it++)
    {
        cout << "key = " << it->first << " value = " << it->second << endl;
    }
    cout << endl;
}

//大小
void test01()
{
    //创建map容器
    map<int, int>m;

    m.insert(pair<int, int>(1, 10));  //1为key;10为value
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(2, 20));

    printMap(m);

    if (m.empty())
    {
        cout << "m为空" << endl;
    }
    else
    {
        cout << "m不为空" << endl;
        cout << "m的大小" << m.size() << endl;
    }
}

//交换
void test02()
{
    map<int, int>m1;

    m1.insert(pair<int, int>(1, 10));  //1为key;10为value
    m1.insert(pair<int, int>(3, 30));
    m1.insert(pair<int, int>(2, 20));

    map<int, int>m2;

    m2.insert(pair<int, int>(4, 100));  
    m2.insert(pair<int, int>(5, 300));
    m2.insert(pair<int, int>(6, 200));

    cout << "交换前:" << endl;
    printMap(m1);
    printMap(m2);

    m1.swap(m2);
    cout << "交换后:" << endl;
    printMap(m1);
    printMap(m2);
}

int main() 
{
    test01();
    test02();

    return 0;
}

运行结果:
在这里插入图片描述

2.5 map容器插入和删除

① 功能描述:map容器进行插入数据和删除数据。
② 函数原型:

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

③ map插入方式很多,记住其一即可。

#include <iostream>
#include <map>

using namespace std;

void printMap(map<int, int>& m)
{
    for (map<int,int>::iterator it = m.begin();it!=m.end();it++)
    {
        cout << "key = " << it->first << " value = " << it->second << endl;
    }
    cout << endl;
}

void test01()
{
    //创建map容器
    map<int, int>m;

    //第一种:
    m.insert(pair<int, int>(1, 10)); 

    //第二种:
    m.insert(make_pair(2, 10));

    //第三种:
    m.insert(map<int, int>::value_type(3, 30));  //map容器下为"值"为(3,30)

    //第四种:
    m[4] = 40;

    cout << m[5] << endl;  //由于没有m[5]没有数,它会自动创建出一个value为0的数
    cout << m[4] << endl;  //不建议用[]插入,但是可以利用key访问到value。

    printMap(m);

    //删除
    m.erase(m.begin());
    printMap(m);

    m.erase(3);  //安装key删除
    printMap(m);

    //清空方式一
    m.erase(m.begin(),m.end());  
    //清空方式二
    m.clear();

    printMap(m);
}

int main() 
{
    test01();

    return 0;
}

运行结果:
在这里插入图片描述

2.6 map容器查找和统计

① 对map容器进行查找数据以及统计数据。
② 函数原型:

1. find(key);  //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
2. cout(key); //统计key的元素个数。
#include <iostream>
#include <map>

using namespace std;

void test01()
{
    //创建map容器
    map<int, int>m;

    m.insert(pair<int,int>(1, 10));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int,int>(2, 20));
    m.insert(pair<int, int>(3, 30));

    map<int,int>::iterator pos = m.find(3);
    
    if (pos != m.end())
    {
        cout << "查到了元素 key = " << (*pos).first << " value = " << pos->second << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }

    //统计
    //map不允许插入重复key元素,count统计而言 结果要么是0 要么是1
    //mutimap 的count统计可以大于1
    int num = m.count(3);
    cout << "num = " << num << endl;
}

int main() 
{
    test01();
    
    return 0;
}

运行结果:
在这里插入图片描述

2.7 map容器排序

① map容器默认排序规则为按照key值进行从小到大排序,利用仿函数,可以改变排序规则。
② 利用仿函数可以指定map容器的排序规则。
③ 对于自定义数据类型,map必须要指定排序规则,同set容器。

#include <iostream>
#include <map>

using namespace std;

class MyCompare
{
public:
    bool operator()(int v1, int v2)const
    {
        //降序
        return v1 > v2;
    }
};

void printMap(map<int, int, MyCompare>& m)
{
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key = " << it->first << " value = " << it->second << endl;
    }
    cout << endl;
}

void test01()
{
    //创建map容器
    //不是插了之后再排序,而是在创建的时候就排序
    map<int, int, MyCompare>m;

    m.insert(make_pair(1, 10));
    m.insert(make_pair(2, 20));
    m.insert(make_pair(3, 30));
    m.insert(make_pair(4, 40));
    m.insert(make_pair(5, 50));

    printMap(m);
}

int main()
{
    test01();

    return 0;
}

运行结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值