【C++】set/multiset容器

1.set基本概念

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//set容器构造和赋值
#include<set>

//遍历
void printSet(const set<int>& st)
{
	for (set<int>::const_iterator it = st.begin(); it != st.end(); it++)
	{
		cout << *it << " ";
	}
	//换行
	cout << endl;
}
//set容器构造和赋值
void test01()
{
	set<int>st1; // 创建set容器
	//插入数据 只有insert方式
	st1.insert(10);
	st1.insert(20);
	st1.insert(50);
	st1.insert(30);
	st1.insert(40);
	st1.insert(30);
	//打印输出
	printSet(st1);
	//set容器特点:所有元素插入时自动被排序
	//set容器不允许插入重复值

	//operator= 赋值
	set<int>st2;
	st2 = st1;
	printSet(st1);

	//拷贝构造
	set<int>st3(st2);
	printSet(st3);
}

int main()
{ 
	test01();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

2.set大小和交换

在这里插入图片描述

在这里插入图片描述

#include <iostream>
using namespace std;

//set容器 大小和交换
#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;
	//set容器只能用insert插入数据
	s1.insert(10);
	s1.insert(50);
	s1.insert(30);
	s1.insert(40);
	s1.insert(20);
	//打印容器
	printSet(s1);

	//判断容器是否为空
	if (s1.empty())
	{
		cout << "s1为空" << endl;
	}
	else
	{
		cout << "s1不为空" << endl;
		cout << "s1大小为 " << s1.size() << endl; //输出s1元素个数
	}

}

//交换
void test02()
{
	//创建set容器1
	set<int>s1;
	s1.insert(10);
	s1.insert(50);
	s1.insert(30);
	s1.insert(40);
	s1.insert(20);

	//创建set容器2
	set<int>s2;
	s2.insert(100);
	s2.insert(400);
	s2.insert(300);
	s2.insert(500);
	s2.insert(200);

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

	//交换s1和s2容器
	cout << "交换后:" << endl;
	printSet(s1);
	printSet(s2);
}

int main()
{ 
	test01();
	cout << "-------------" << endl << endl;
	test02();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

3.set插入和删除

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//set容器 插入和删除
#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容器
	set<int>s1;
	//插入
	s1.insert(50);
	s1.insert(30);
	s1.insert(10);
	s1.insert(20);
	s1.insert(40);
	//打印
	printSet(s1);

	//删除
	s1.erase(s1.begin());
	printSet(s1);

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

	//清空
	//s1.erase(s1.begin(), s1.end()); //利用区间的方式
	s1.clear(); // 利用clear()成员函数
	printSet(s1);
}

int main()
{ 
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

4.set查找和统计

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//set容器 查找和统计
#include<set>
void test01()
{
	set<int>s1;
	s1.insert(30);
	s1.insert(20);
	s1.insert(50);
	s1.insert(10);
	s1.insert(40);

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

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

int main()
{ 
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

5.set和multiset区别

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//set容器 和multiset容器的区别
#include <set>
void test01()
{
	//创建set容器
	set<int>s;

	//pair<iterator, bool> 使用insert返回值类型
	pair<set<int>::iterator, bool> ret = s.insert(10);

	if (ret.second)
	{
		cout << "第一次插入成功  " << "插入的数据为:" <<  *ret.first << endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}
	
	//再次插入相同的数
	ret = s.insert(10);
	if (ret.second)
	{
		cout << "第一次插入成功" << *ret.first << endl;
	}
	else
	{
		cout << "第一次插入失败" << endl;
	}

	//创建multiset
	multiset<int>ms;
	//允许插入重复值
	ms.insert(20);
	ms.insert(20);
	for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main()
{ 
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

6.pair对组创建

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

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

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

int main()
{
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述

7.set容器排序

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

//set容器排序
#include <set>

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

void test01()
{
	set<int, MyCompare>s1;
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	s1.insert(10);
	//从大到小排序
	for (set<int, MyCompare>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main()
{
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;

// set容器排序, 存放自定义数据类型
#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)
	{
		//按照年龄进行排序 降序
		return p1.m_Age > p2.m_Age;
	}
};

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

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

	//将数据插入容器中
	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);

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

int main()
{
	test01();
	//cout << "-------------" << endl << endl;
	//test02();

	//**************************************
	system("pause");
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值