【C++】set/multiset容器

1.set基本概念



cpp 复制代码
#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大小和交换

cpp 复制代码
#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插入和删除


cpp 复制代码
#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查找和统计


cpp 复制代码
#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区别


cpp 复制代码
#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对组创建


cpp 复制代码
#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容器排序



cpp 复制代码
#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;
} 


cpp 复制代码
#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;
} 
相关推荐
码云数智-大飞几秒前
OpCache 原理深挖:从字节码缓存到预加载(Preloading)的实战配置
java·开发语言
handler013 分钟前
进程状态流转的本质:Linux 内核队列与底层数据结构解密
linux·运维·c语言·数据结构·c++·笔记·学习
啊我不会诶12 分钟前
2024北京市赛补题
c++·算法
tjl521314_2123 分钟前
01C++ 分离编译与多文件编程
前端·c++·算法
cany100024 分钟前
C++ -- 泛型编程
java·开发语言·c++
格林威27 分钟前
面阵相机 vs 线阵相机:堡盟与海康相机选型差异全解析 附C++ 实战演示
开发语言·c++·人工智能·数码相机·计算机视觉·视觉检测·工业相机
样例过了就是过了42 分钟前
LeetCode热题100 单词拆分
c++·算法·leetcode·动态规划·哈希算法
时空系1 小时前
第7篇功能——打造你的工具箱 python中文编程
开发语言·python·ai编程
shughui1 小时前
2026最新JDK版本选择及下载安装详细图文教程【windows、mac附安装包】
java·linux·开发语言·windows·jdk·mac
王老师青少年编程1 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【跳跃与过河问题】:跳跳!
c++·算法·贪心·csp·信奥赛·跳跃与过河问题·跳跳