2021-03-19-C++学习之18-set、map

一、set/multiset 容器
1. set基本概念

  • 所有元素都会在插入时自定被排序。
  • 本质:set/multiset属于关联式容器,底层结构是用二叉树实现。
  • set和multiset区别:
    • set不允许容器中有重复的元素
    • multiset允许容器中有重复的元素

2. set构造和赋值

构造:

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

赋值:

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

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<set>
#include<algorithm> //标准算法头文件

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

void test01()
{
	set<int>s1;
	//插入数据时只有insert方式
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	s1.insert(20); //多插了一个20,而且是无序的

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

	//拷贝构造
	set<int>s2(s1);
	//10 20 30 40
	printSet(s2);

	//赋值
	set<int>s3;
	s3 = s2;
	//10 20 30 40
	printSet(s3);
}

int main()
{
	test01();
	
	system("pause");
	return 0;
}

3. set大小和交换
函数原型:
在这里插入图片描述
set不允许重新指定元素个数(即不支持resize())。
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<set>
#include<algorithm> //标准算法头文件

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

void test01()
{
	set<int>s1;
	//插入数据时只有insert方式
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	s1.insert(20); //多插了一个20,而且是无序的

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

void test02()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);

	set<int>s2;
	s2.insert(100);
	s2.insert(400);
	s2.insert(300);
	s2.insert(200);
	cout << "--------交换前--------" << endl;
	cout << "s1:";
	printSet(s1);
	cout << "s2:";
	printSet(s2);

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

int main()
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

输出:

s1不为空
s1大小为:4
--------交换前--------
s1:10 20 30 40
s2:100 200 300 400
--------交换后--------
s1:100 200 300 400
s2:10 20 30 40
请按任意键继续. . .
  1. set插入和删除
    函数原型:
    在这里插入图片描述
    代码测试:
#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<set>
#include<algorithm> //标准算法头文件

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

void test01()
{
	set<int>s1;
	//插入数据时只有insert方式
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	s1.insert(20); //多插了一个20,而且是无序的

	//遍历
	//10 20 30 40
	printSet(s1);

	//删除
	//20 30 40
	s1.erase(s1.begin());
	printSet(s1);

	//删除的重载版本
	s1.erase(30);
	printSet(s1);

	//清空
	//s1.erase(s1.begin(), s1.end());
	s1.clear();
	printSet(s1);
}


int main()
{
	test01();
	
	system("pause");
	return 0;
}

输出:

10 20 30 40
20 30 40
20 40

请按任意键继续. . .

5. set查找和统计
函数原型:

  • find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
  • count(key); //统计key的元素个数

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<set>
#include<algorithm> //标准算法头文件

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

void test01()
{
    set<int>s1;
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);

    set<int>::iterator pos = s1.find(30);
    if (pos != s1.end())
    {
        cout << "找到元素:" << *pos << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }
}
//统计
void test02()
{
    set<int>s1;
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    s1.insert(30);
    s1.insert(30);

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

int main()
{
    test01();
    test02();
    
    system("pause");
    return 0;
}

输出:

找到元素:30
num = 1
请按任意键继续. . .

6. set和multiset区别
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<set>
#include<algorithm> //标准算法头文件

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

void test01()
{
    set<int>s1;
    //set用insert插入会返回对组
    pair<set<int>::iterator, bool> ret = s1.insert(10);
    //第一次插入成功
    if (ret.second)
    {
        cout << "第一次插入成功" << endl;
    }
    else
    {
        cout << "第一次插入失败" << endl;
    }

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

    multiset<int>ms;
    ms.insert(10);
    ms.insert(10);
    ms.insert(10);
    ms.insert(10);

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

int main()
{
    test01();
    
    system("pause");
    return 0;
}

输出:

第一次插入成功
第二次插入失败
10 10 10 10
请按任意键继续. . .

7. pair对组创建
功能描述:

  • 成对出现的数据,利用对组可以返回两个数据

两种创建方式:

  • pair<type,type> p(value1,value2);
  • pair<type,type> p = make_pair(value1,value2);

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<set>
#include<algorithm> //标准算法头文件

void test01()
{
   //第一种方式
    pair<string, int>p("Tom", 20);
    cout << "姓名:" << p.first << " 年龄:" << p.second << endl;

   //第二种方式
    pair<string, int>p2 = make_pair("Jerry", 30);
    cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}

int main()
{
    test01();
    
    system("pause");
    return 0;
}

输出:

姓名:Tom 年龄:20
姓名:Jerry 年龄:30
请按任意键继续. . .

8. set容器排序

  • set容器默认排序规则为从小到大,如何改变排序规则?
  • 利用仿函数,可以改变排列规则。

代码测试1:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<set>
#include<algorithm> //标准算法头文件

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

void test01()
{
    set<int>s1;
    s1.insert(10);
    s1.insert(40);
    s1.insert(20);
    s1.insert(50);
    s1.insert(30);

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

    //指定排列规则为从大到小
    set<int, MyCompare>s2;
    s2.insert(10);
    s2.insert(40);
    s2.insert(20);
    s2.insert(50);
    s2.insert(30);

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

int main()
{
    test01();
    
    system("pause");
    return 0;
}

输出:

10 20 30 40 50
50 40 30 20 10
请按任意键继续. . .

代码测试2:set存放自定义数据类型

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<set>
#include<algorithm> //标准算法头文件

//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();
    
    system("pause");
    return 0;
}

输出:

姓名:关羽 年龄:28
姓名:张飞 年龄:25
姓名:刘备 年龄:24
姓名:赵云 年龄:21
请按任意键继续. . .

二、map/multimap容器
1. map基本概念

  • 简介:
    • map中所有元素都是pair
    • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
    • 所有元素都会根据元素的键值自动排序
      -本质:
    • map/multimap属于关联式容器,底层结构是用二叉树实现
  • 优点:
    • 可以根据key值快速找到value值
  • map和multimap区别:
    • map不允许容器中有重要的key值元素
    • multimap允许容器中有重复key值元素

2. map构造和赋值
函数原型:

  • 构造:
    • map<T1, T2> mp; //map默认构造函数
    • map(const map &mp); //拷贝构造函数
  • 赋值:
    • map& operator=(const map &mp); //重载等号操作符

代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<map>
#include<algorithm> //标准算法头文件

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<int, int> m;
    //pair<int, int>(1, 10)是匿名对组
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    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();
    
    system("pause");
    return 0;
}

输出:

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

请按任意键继续. . .

3. map大小和交换
函数原型:
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<map>
#include<algorithm> //标准算法头文件

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<int, int>m;
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    if (m.empty())
    {
        cout << "m为空" << endl;
    }
    else
    {
        cout << "m不为空" << endl;
        cout << "m的大小为:" << m.size() << endl;
    }
}
//交换
void test02()
{
    map<int, int>m;
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));

    map<int, int>m2;
    m2.insert(pair<int, int>(4, 40));
    m2.insert(pair<int, int>(5, 50));
    m2.insert(pair<int, int>(6, 60));

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

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

int main()
{
    test01();
    test02();
    
    system("pause");
    return 0;
}

输出:

m不为空
m的大小为:3
交换前:
--------m--------
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30

--------m2--------
key = 4 value = 40
key = 5 value = 50
key = 6 value = 60

交换后:
--------m--------
key = 4 value = 40
key = 5 value = 50
key = 6 value = 60

--------m2--------
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30

请按任意键继续. . .

4. map插入和删除
函数原型:
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<map>
#include<algorithm> //标准算法头文件

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<int, int>m;
    //插入
    //第一种
    m.insert(pair<int, int>(1, 10));

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

    //第三种
    m.insert(map<int, int>::value_type(3, 30));

    //第四种
    m[4] = 40;
    //m[5]未创建,但是输出的是:key = 5 value = 0
    //[]不建议插入,用途 可以利用key访问到value
    //cout << m[5] << endl;
    cout << m[4] << endl;
    printMap(m);

    //删除
    m.erase(m.begin());
    printMap(m);
    //只能按照key删除
    m.erase(3); //按照 key = 3 删除
    printMap(m);

    //清空
    //m.erase(m.begin(), m.end());
    //或者
    m.clear();
    printMap(m);
}

int main()
{
    test01();
    
    system("pause");
    return 0;
}

输出:

40
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

key = 2 value = 20
key = 3 value = 30
key = 4 value = 40

key = 2 value = 20
key = 4 value = 40


请按任意键继续. . .

5. map查找和统计
函数原型:
在这里插入图片描述
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<map>
#include<algorithm> //标准算法头文件

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<int, int>m;
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(3, 40));
    
    map<int, int>::iterator pos = m.find(3);
    if (pos != m.end())
    {
        cout << "查到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }

    //统计
    //不是0就是1
    //map不允许插入重复key 元素
    //multimap的count统计可能大于1
    int num = m.count(3);
    cout << "num = " << num << endl; //num = 1
   
}

int main()
{
    test01();
    
    system("pause");
    return 0;
}

输出:

查到了元素 key = 3 value = 30
num = 1
请按任意键继续. . .
  • 总结:
    • 查找 – find (返回的是迭代器)
    • 统计 – count (对于map,结果为0或者1)

6. map容器排序
技术点:利用仿函数,可以改变排列规则
代码测试:

#include<iostream>
using namespace std;
#include<string>
//STL中的每个容器在使用时都需要包含头文件
#include<map>
#include<algorithm> //标准算法头文件

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

void test01()
{
    //查找
    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));
   
    for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key = " << it->first << " value = " << it->second << endl;
    }
}

int main()
{
    test01();
    
    system("pause");
    return 0;
}

输出:

key = 5 value = 50
key = 4 value = 40
key = 3 value = 30
key = 2 value = 20
key = 1 value = 10
请按任意键继续. . .

对于自定义类型,map必须要指定排列规则,同set容器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力学习的代码小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值