C++——Set和Multiset用法使用总结

一丶Set容器介绍

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

二丶Set容器的构造函数和赋值函数

函数原型

set<type> setName;		//默认的构造函数
set(const set& se);		//拷贝构造函数
set& operator=(const set& se);		//重载操作符=

案例

#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>& s){
	for (set<int>::iterator ptr = s.begin(); ptr != s.end();ptr++)
	{
		cout << *ptr << " ";
	}
	cout << endl;
}
void test01(){
	set<int> s1;
	s1.insert(10);
	s1.insert(1);
	s1.insert(30);
	s1.insert(5);
	cout << "s1为:\t";
	printSet(s1);
	set<int>s2 = s1;
	cout << "s2为:\t";
	printSet(s2);
	set<int>s3(s2);
	cout << "s3为:\t";
	printSet(s3);
}
void main(){
	test01();
	system("pause");
	return;
}

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

三丶Set容器的大小和交换

函数原型

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

案例

#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>& s){
	for (set<int>::iterator ptr = s.begin(); ptr != s.end();ptr++)
	{
		cout << *ptr << " ";
	}
	cout << endl;
}
void test01(){
	set<int> s1;
	s1.insert(10);
	s1.insert(1);
	s1.insert(30);
	s1.insert(5);
	cout << "s1为:";
	printSet(s1);
	cout << "s1中的元素个数为:" << s1.size() << endl;
	if (s1.empty())
	{
		cout << "为空" << endl;
	}
	else
	{
		cout << "不为空" << endl;
	}
	set<int> s2;
	s2.insert(101);
	s2.insert(11);
	s2.insert(301);
	s2.insert(51);
	s2.insert(5);
	cout << "s2为:";
	printSet(s2);
	s1.swap(s2);
	cout << "交换之后为:" << endl;
	cout << "s1为:";
	printSet(s1);
	cout << "s2为:";
	printSet(s2);
}
void main(){
	test01();
	system("pause");
	return;
}

运行结果为:

在这里插入图片描述

四丶Set容器的插入和删除

函数原型

insert(elem);				//插入elem元素
clear();					//清空set容器的元素
erase(beg,end);				//删除区间[beg,end)的元素
erase(pos);					//删除迭代器pos位置的元素,返回下一个元素的位置
erase(elem);				//删除容器中所有跟elem值匹配的成员

案例

#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>& s){
	for (set<int>::iterator ptr = s.begin(); ptr != s.end();ptr++)
	{
		cout << *ptr << " ";
	}
	cout << endl;
}
void test01(){
	set<int> s1;
	s1.insert(10);
	s1.insert(1);
	s1.insert(30);
	s1.insert(5);
	s1.insert(101);
	s1.insert(11);
	s1.insert(302);
	s1.insert(53);
	cout << "s1为:";
	printSet(s1);

	s1.erase(s1.begin());
	cout << "************" << endl;
	cout << "s1为:";
	printSet(s1);

	s1.erase(11);
	cout << "************" << endl;
	cout << "s1为:";
	printSet(s1);

	s1.erase(s1.begin(),(++(++s1.begin())));
	cout << "************" << endl;
	cout << "s1为:";
	printSet(s1);

	s1.clear();
	cout << "************" << endl;
	cout << "s1为:";
	printSet(s1);
}
void main(){
	test01();
	system("pause");
	return;
}

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

五丶Set容器查找和统计

函数原型

find(elem);			//查找elem是否存在,存在则返回该元素的迭代器,如果不存在则返回最后一个位置。
count(elem);		//统计elem元素的个数

案例

#include<iostream>
using namespace std;
#include<set>
void printSet(const set<int>& s){
	for (set<int>::iterator ptr = s.begin(); ptr != s.end();ptr++)
	{
		cout << *ptr << " ";
	}
	cout << endl;
}
void test01(){
	set<int> s1;
	s1.insert(10);
	s1.insert(1);
	s1.insert(30);
	s1.insert(5);
	s1.insert(10);
	s1.insert(10);
	cout << "s1为:";
	printSet(s1);

	cout << "**********************" << endl;
	cout << "s1中10的个数为:\t" << s1.count(10) << endl;
	cout << "s1中100的个数为:\t" << s1.count(100) << endl;

	cout << "**********************" << endl;
	set<int>::iterator ptr = s1.find(10);
	if (ptr != s1.end())
	{
		cout << "已查找到元素:" << *ptr << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
	ptr = s1.find(105);
	if (ptr != s1.end())
	{
		cout << "已查找到元素:" << *ptr << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
	
}
void main(){
	test01();
	system("pause");
	return;
}

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

六丶Set容器和Multiset容器

multiset容器的操作和set容器基本相似,但是multiset容器可以插入重复的值。
案例

#include<iostream>
using namespace std;
#include<set>
void test02(){
	multiset<int>ms;
	ms.insert(10);
	ms.insert(20);
	ms.insert(30);
	ms.insert(40);
	ms.insert(10);
	ms.insert(10);
	for (multiset<int>::iterator ptr = ms.begin(); ptr != ms.end(); ptr++)
	{
		cout << *ptr << " ";
	}
	cout << endl;
}
void main(){
	test02();
	system("pause");
	return;
}

运行结果为:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值