【C++】黑马C++泛型编程和STL技术 (7) STL常用容器 ---set/multiset

3.8 set/multiset容器

3.8.1 基本概念

集合式容器,所有元素会在插入时自动排序

属于关联式容器,底层结构用二叉树实现。

set不允许容器中有重复的元素,而multiset允许容器中有重复的元素。头文件只需要包含<set>就可以使用这两个容器。

3.8.2 set构造和赋值

构造:

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

赋值

set operator=(const set &st);//重载

示例

#include<iostream>
using namespace std;
#include<set>
// set的构造和赋值
void printSet(const set<int> s)
{
	for(set<int>::const_iterator it=s.begin();it!=s.end();it++)
	{
		cout << *it <<" ";
	}
	cout << endl;
}
void test01()
{
	set<int> s1;
	// 插入数据时只有insert
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	s1.insert(10);
	printSet(s1);
	// 拷贝构造
	set<int> s2(s1);
	printSet(s2);
	// 赋值
	set<int> s3;
	s3 = s2;
	printSet(s3);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
插入的顺序是 10 30 20 40 10,但是输出结果时10 20 30 40,即自动排序,并且不允许有重复数据的插入。

3.8.3 set大小和交换

函数原型:

size(); 	// 返回容器中元素的数目
empty(); 	// 判断容器是否为空
swap(st);	// 交换两个集合容器

这里没有resize操作,因为resize填充的数据是一样的,而set容器不允许重复数据插入。
示例:

#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int> s)
{
	for(set<int>::const_iterator it=s.begin();it!=s.end();it++)
	{
		cout << *it <<" ";
	}
	cout << endl;
}
void test01()
{
	set<int> s1;
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(10);
	if(s1.empty())
		cout << "s1为空"<< endl;
	else
		cout << "s1不为空,大小为:" << s1.size() << endl;
	printSet(s1);
	set<int> s2;
	s2.insert(400);
	s2.insert(200);
	s2.insert(300);
	s2.insert(100);
	printSet(s2);
	s1.swap(s2);
	cout << "交换后:"<<endl;
	printSet(s1);
	printSet(s2);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

运行结果:

在这里插入图片描述

3.8.4 set插入和删除

函数原型:

insert(elem);
clear();
erase(pos);			// 删除pos位置所指的元素,返回下一个元素的迭代器
erase(beg,end);		// 删除区间(左闭右开)的元素,返回下一个元素的迭代器
erase(elem);		// 删除容器中值为elem的元素

示例:

#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int> &s)
{
	for(set<int>::const_iterator it = s.begin();it!=s.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test01()
{
	set<int> s1;
	// 插入
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(10);
	printSet(s1);
	// 删除
	set<int>::iterator it = s1.begin();
	s1.erase(++it);
	printSet(s1);
	s1.erase(10);
	printSet(s1);
	s1.clear();
	printSet(s1);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
空的那一行就表示清空之后,只打印输出了一个换行。

3.8.5 set查找和统计

函数原型

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

示例:

#include<iostream>
using namespace std;
#include<set>
void test01()
{
	set<int> s1;
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(10);
	// find
	set<int>::iterator pos = s1.find(30);
	if(pos!=s1.end())
	{
		cout << "找到元素" << *pos << endl;
	}
	else
		cout << "未找到! " << endl;
	// count
	cout << s1.count(100) << endl;
	multiset<int> s2;
	s2.insert(10);
	s2.insert(10);
	s2.insert(10);
	s2.insert(10);
	s2.insert(10);
	cout << s2.count(10) << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

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

3.8.6 set和multiset的区别

set不可以插入重复数据,而multiset可以。
set插入数据的同时会返回插入结果,表示插入是否成功。
multiset不会检测数据,因此可以插入重复数据。

示例:

#include<iostream>
using namespace std;
#include<set>
void test01()
{
	set<int> s1;
	//insert 返回值类型是 pair<iterator,bool>,bool代表插入结果
	pair<set<int>::iterator,bool> ret = s1.insert(10);
	// ret.second 是访问返回之中的bool类型
	if(ret.second)
	{
		cout << "第一次插入成功!" << endl;
	}
	else
	{
		cout << "第一次插入失败!" << endl;
	}
	ret = s1.insert(10);
	if(ret.second)
	{
		cout << "第二次插入成功!" << endl;
	}
	else
	{
		cout << "第二次插入失败!" << endl;
	}
	multiset<int> s2;
	s2.insert(10); // 返回值只有迭代器
	s2.insert(10);
	for(multiset<int>::iterator it =s2.begin();it!=s2.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

运行结果:

在这里插入图片描述

3.8.7 pair对组创建

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

两种创建方式:

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

示例:

#include<iostream>
using namespace std;
void test01()
{
	// 方式一
	pair<string,int> p("TOM",20);
	cout << "姓名:"<< p.first << " " << "年龄:" << p.second<< endl;
	// 方式二
	pair<string,int> p2 = make_pair("JERRY",19);
	cout << "姓名:"<< p2.first << " " << "年龄:" << p2.second<< endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

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

3.8.8 set容器排序

默认排序规则是升序,那么如何改变此规则?以及如果对自定义数据类型进行排序?
这里主要是利用仿函数(函数调用运算符()的重载)来实现。

示例一:set存放内置数据类型

#include<iostream>
using namespace std;
#include<set>
// 1. set存放内置数据类型
// 仿函数 ==> 指定排序规则
class myCompare
{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
		// 因为想让其降序排列,所以当v1>v2时返回值为true		
	}
};
void test01()
{
	set<int> s1;
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(10);
	s1.insert(50);
	cout << "默认的升序:" << endl;
	for(set<int>::iterator it = s1.begin();it!=s1.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	// 指定排序规则
	// 因为插入数据之后,set容器已经自动排序
	// 所以应该在创建容器的时候给定排序规则,而不是在插入数据之后
	// 利用仿函数实现
	set<int,myCompare> s2;
	s2.insert(40);
	s2.insert(20);
	s2.insert(30);
	s2.insert(10);
	s2.insert(50);
	cout << "修改后的降序:" << endl;
	for(set<int,myCompare>::iterator it = s2.begin();it!=s2.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

创建容器时参数列表改变了,这就是一个新的容器,需要新的迭代器来进行遍历
运行结果:

在这里插入图片描述

示例二:set存放自定义数据类型

set容器中,自定义数据类型必须先确定排序规则,然后才可以插入数据

#include<iostream>
using namespace std;
#include<set>
#include<string>
class Person
{
public:
	Person(string name,int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
// 仿函数 === 定义排序规则
class cmpPerson
{
public:
	bool operator()(const Person &p1,const Person &p2)
	{
		// 按照年龄降序
		return p1.m_Age > p2.m_Age;
	}
};
void test01()
{
	set<Person,cmpPerson> s;
	// 准备数据
	Person p1("刘备",30);
	Person p2("关羽",28);
	Person p3("张飞",24);
	Person p4("赵云",20);
	Person p5("孔明",40);
	// 插入数据
	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	s.insert(p5);
	// 遍历
	for(set<Person,cmpPerson>::iterator it = s.begin();it!=s.end();it++)
	{
		cout << "姓名:" << it->m_Name << " "
			<< "年龄:" << it->m_Age << endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值