32,set/multiset容器

32.1set基本概念

简介:所有元素都会在插入时自动被排序

本质:set/multiset属于关联式容器,底层结构是用二叉树实现

set和multiset区别:

set不允许容器中有重复的元素

multiset允许容器中有重复的元素

32.2set构造和赋值

功能描述:创建set容器以及赋值

构造:

set<T>st;                                  //默认构造函数

set(const set &st);                    //拷贝构造函数

赋值:

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

#include<iostream>
using namespace std;
#include<set>
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 = { 1,6,7,3,8,2,5,};
	printSet(s1);
	//输出结果:1 2 3 5 6 7 8

	s1.insert(4);
	printSet(s1);
	//输出结果:1 2 3 4 5 6 7 8

	s1.insert(4);
	printSet(s1);
	//输出结果:1 2 3 4 5 6 7 8
	//set容器不能重复,不会报错,但是重复了就插入不成功

	set<int>s2(s1);
	printSet(s2);
	//输出结果:1 2 3 4 5 6 7 8

	set<int>s3;
	s3 = s1;
	printSet(s3);
	//输出结果:1 2 3 4 5 6 7 8
}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结:

set容器插入数据时用inset或=,不用push等

set容器插入数据的数据会自动排序

32.3set大小和交换

功能描述:

统计set容器大小以及交换set容器

函数原型:

size();        //返回容器中元素的数目

empty();     //判断容器是否为空

swap(st);    //交换两个集合容器

#include<iostream>
using namespace std;
#include<set>
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 = { 1,6,7,3,8,2,5,4 };
	printSet(s1);
	//输出结果:1 2 3 4 5 6 7 8

	if (!s1.empty())
	{
		cout << "s1不为空" << endl;
		cout << "s1大小:" << s1.size() << endl;
	}
	else
	{
		cout << "s1为空" << endl;
	}
	//输出结果:s1不为空
	//          s1大小:8

	set<int>s2;
	s2 = { 10,30,50,80,20 };
	cout << "交换前s1内元素:" << endl;
	printSet(s1);
	cout << "交换前s2内元素:" << endl;
	printSet(s2);
	/*输出结果:交换前s1内元素:
	            1 2 3 4 5 6 7 8
	   	        交换前s2内元素:
		        10 20 30 50 80*/

	s1.swap(s2);
	cout << "交换后s1内元素:" << endl;
	printSet(s1);
	cout << "交换后s2内元素:" << endl;
	printSet(s2);
	/*输出结果:交换后s1内元素:
                10 20 30 50 80
                交换后s2内元素:
                1 2 3 4 5 6 7 8*/
}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结:

统计大小--size

判断是否为空--empty

交换容器--swap

32.4set插入和删除

功能描述:

set容器进行插入数据和删除数据

函数原理:

insert(elem);       //在容器中插入元素

clear();                //清除所有元素

erase(pos);         //删除pos迭代器所指的元素,返回下一个元素的迭代器

erase(beg,end);  //删除区间[beg,end)的所有元素,返回下一个元素的迭代器

erase(elem);       //删除容器中值为elem的元素

#include<iostream>
using namespace std;
#include<set>
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 = { 1,6,7,3,8,2,5,4 };
	printSet(s1);
	//输出结果:1 2 3 4 5 6 7 8

	s1.insert(9);
	printSet(s1);
	//输出结果:1 2 3 4 5 6 7 8 9

	s1.erase(s1.begin());
	printSet(s1);
	//输出结果:2 3 4 5 6 7 8 9

	s1.erase(4);
	printSet(s1);
	//输出结果:2 3 5 6 7 8 9

	set<int>::iterator it = s1.begin();
	s1.erase(++it, s1.end());
	printSet(s1);
	//输出结果:2

	s1.clear();
	if (s1.empty())
	{
		cout << "s1为空" << endl;
	}
	else
	{
		cout << "s1不为空" << endl;
	}
	//输出结果:s1为空
}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结:

插入--insert

删除--erase

清空--clear

注意:

end()指向的是最后一位元素的下一位

因为实际应用的时候确实需要这个指向最后元素下一个的end()。比如find的时候,找到了给你返回pair(里面是key和值),找不到的时候给你返回什么呢?比如一个只有一个元素的容器,那begin()和end()不是指向相同了,这样无法写出for循环了吧?虽然这些问题可以用一些手段解决,但是不通用不简洁,所以end()指向结尾后一个是很合理的

32.5set查找和统计

功能描述:

对set容器进行查找数据以及统计数据

函数原型:

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

count(key);   //统计key的元素个数(set中值只会是0或1)

#include<iostream>
using namespace std;
#include<set>
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 = { 1,6,7,3,8,2,5,4 };
	printSet(s1);
	//输出结果:1 2 3 4 5 6 7 8

	set<int>::iterator pos = s1.find(3);
	if (pos != s1.end())
	{
		cout << "找到元素:" << *pos << endl;
	}
	else
	{
		cout << "没有找到元素" << endl;
	}
	//输出结果:找到元素:3

	cout << "元素个数:" << s1.count(3) << endl;
	//输出结果:元素个数:1
}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结:

查找--find(返回的是迭代器)

统计--count(对于set,结果0或者1)

32.6set和multiset区别

学习目标:

掌握set和multiset的区别

区别:

set不可以插入重复数据,而multiset可以

set插入数据的同时会返回插入结果,表示插入是否成功

multiset不会检测数据,因此可以插入重复数据

#include<iostream>
using namespace std;
#include<set>
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void printMultiset(multiset<int>&s)
{
	for (multiset<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int>s1;
	s1 = { 1,6,7,3,8,2,5,4 };
	pair<set<int>::iterator, bool> ret1 = s1.insert(9);
	if (ret1.second)
	{
		cout << "第一次插入成功"<< endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}
	//输出结果:第一次插入成功
	pair<set<int>::iterator, bool> ret2 = s1.insert(9);
	if (ret2.second)
	{
		cout << "第二次插入成功" << endl;
	}
	else
	{
		cout << "第二次插入失败" << endl;
	}
	//输出结果:第二次插入失败
	printSet(s1);
	//输出结果:1 2 3 4 5 6 7 8 9

	multiset<int>s2;
	s2 = { 1,6,7,3,8,2,5,4 };
	s2.insert(9);
	s2.insert(9);
	printMultiset(s2);
	//输出结果:1 2 3 4 5 6 7 8 9 9
}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结:

如果不允许插入重复数据可以利用set

如果需要插入重复数据利用multiset

32.7pair对组创建

功能描述:成对出现的数据,利用对组可以返回两个数据

两种创建方式:
pair<type,type>p(value1,value2);

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

#include<iostream>
using namespace std;
void test01()
{
	pair<string,int>p1("Tom", 20);
	cout << "姓名:" << p1.first << " 年龄:" << p1.second << endl;

	pair<string, int>p2 = make_pair("Jacke", 30);
	cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结:两种方式都可以创建对组,记住一种即可

32.8set容器排序

学习目标:

set容器默认排序规则为从小到大,掌握如何改变排序规则

主要技术点:

利用仿函数,可以改变排序规则

32.8.1set存放内置数据类型

#include<iostream>
using namespace std;
#include<set>
class MyCompare
{
public:
	bool operator()(const int &v1,const int &v2)const
	{
		return v1 > v2;
	}
	//bool operator()(int v1,int v2)vs2019会报错
	//改成 bool operator()(const int& v1, const int& v2) const 就不会了
};
void test01()
{
	set<int>s1;
	s1.insert(1);
	s1.insert(4);
	s1.insert(2);
	s1.insert(5);
	s1.insert(3);
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//输出结果:1 2 3 4 5

	//指定排序规则为从大到小
    //必须在插入元素之前就指定规则
	set<int,MyCompare>s2;
	s2.insert(1);
	s2.insert(4);
	s2.insert(2);
	s2.insert(5);
	s2.insert(3);

	//遍历
	for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//输出结果:5 4 3 2 1
}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结:利用仿函数可以指定set容器排序规则

32.8.2set存放自定义数据类型

#include<iostream>
using namespace std;
#include<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 p1("张三", 10);
	Person p2("李四", 20);
	Person p3("王五", 30);
	Person p4("赵六", 40);
	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;
}

函数被用作STL容器的比较函数时,我们希望该函数不会修改对象的状态,因为这可能会导致容器的行为不可预测。因此,为了保证该函数不会修改对象的状态,需要在函数声明后加上const

总结:对于自定义数据类型,set必须指定排序规则才可以插入数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值