【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;
} 
相关推荐
郝学胜_神的一滴8 小时前
CMake 034:生成器表达式:解耦构建时序、精简分支逻辑的终极利器
c++·cmake
见过夏天1 天前
C++ 基础入门完全指南
c++
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK3 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境3 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境3 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
AsulTop4 天前
精简版 OpenWrt/LEDE uhttpd/rpc/mod-rpc/ Ubus Json-RPC 从0修复直到可用
rpc·路由器·openwrt·lede·uhttpd·ubus修复
郝学胜_神的一滴4 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境6 天前
C++ 的Eigen 库全解析
c++
卷无止境6 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端