C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组

目录

  • set/multiset 容器
    • set 构造和赋值
    • set容器的大小和交换
    • set容器的插入和删除
    • set容器的查找和统计
    • set和multiset区别
    • 注:pair对组
    • 改变set的排序规则
    • set添加自定义的数据类型排序
  • map/ multimap容器
    • 基本概念
    • map的构造和赋值
    • map的大小和交换
    • map的插入和删除
    • map容器查找和统计
    • map容器排序
  • 综合案例:员工分组

set/multiset 容器

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第1张图片

set 构造和赋值

//构造和赋值
void test01()
{
	set<int> s1;

	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1);

	//拷贝构造
	set<int>s2(s1);
	printSet(s2);

	//赋值
	set<int>s3;
	s3 = s2;
	printSet(s3);
}

set容器的大小和交换

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第2张图片

s1.size()
s1.empty() //判空 bool返回 
s1.swap(s2); //交换s1和s2两个容器

set容器的插入和删除

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第3张图片

//插入和删除
void test01()
{
	set<int> s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	printSet(s1); //10 20 30 40  自动排序

	//删除
	s1.erase(s1.begin());
	printSet(s1); //10 20 30

	s1.erase(30);
	printSet(s1); //10 20

	//清空
	//s1.erase(s1.begin(), s1.end());
	s1.clear();
	printSet(s1);//空
}

set容器的查找和统计

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第4张图片

//查找和统计
void test01()
{
	set<int> s1;
	//插入
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	
	//查找   
	set<int>::iterator pos = s1.find(30);

	if (pos != s1.end())
	{
		cout << "找到了元素 : " << *pos << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

	//统计
	int num = s1.count(30); // 统计30出现的次数   只能是0或1
	cout << "num = " << num << endl;
}

set和multiset区别

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第5张图片

#include 

//set和multiset区别
void test01()
{
	set<int> s;
	// pair会在下面介绍到  对组,这里第一个元素为set的迭代器,第二个元素为bool类型
	pair<set<int>::iterator, bool>  ret = s.insert(10);
	if (ret.second) {
		cout << "第一次插入成功!" << endl;
	}
	else {
		cout << "第一次插入失败!" << endl;
	}

	ret = s.insert(10);
	if (ret.second) {
		cout << "第二次插入成功!" << endl;
	}
	else {
		cout << "第二次插入失败!" << endl;
	}
    
	//multiset
	multiset<int> ms;
	ms.insert(10);
	ms.insert(10);

	for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

注:pair对组

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第6张图片

#include 

//对组创建
void test01()
{
	// 创建一个对组
	pair<string, int> p(string("Tom"), 20);
	// 访问第一个元素是p.first   第二个元素是p.second
	cout << "姓名: " <<  p.first << " 年龄: " << p.second << endl;

	pair<string, int> p2 = make_pair("Jerry", 10);
	cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}

改变set的排序规则

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第7张图片

#include 

// 自定义排序函数
class MyCompare 
{
public:
	bool operator()(int v1, int v2) {
		return v1 > v2;
	}
};
void test01() 
{    
	set<int> s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(50);

	//默认从小到大
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
		cout << *it << " ";  //10 20 30 40 50
	}

	//指定排序规则
	set<int,MyCompare> s2;
	s2.insert(10);
	s2.insert(40);
	s2.insert(20);
	s2.insert(30);
	s2.insert(50);

	for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
		cout << *it << " ";   //50 40 30 20 10
	}
	cout << endl;
}

set添加自定义的数据类型排序

#include 
#include 

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容器中
	set<Person, comparePerson> s;

	Person p1("刘备", 23);
	Person p2("关羽", 27);
	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 << " 年龄: " << it->m_Age << endl;
	}
}

map/ multimap容器

基本概念

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第8张图片

map的构造和赋值

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第9张图片

void test01()
{
	map<int,int>m; //默认构造
	// 插入对组
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));
	printMap(m);
	
	//拷贝构造
	map<int, int>m2(m); 
	printMap(m2);
	
	//赋值
	map<int, int>m3;
	m3 = m2; 
	printMap(m3);
}

map的大小和交换

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第10张图片

m.swap(m2); //交换m和m2
m.empty(); // 返回bool
m.size()// map容器大小

map的插入和删除

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第11张图片

//插入方式
m.insert(pair<int, int>(1, 10));

//删除第一个元素
m.erase(m.begin());

//删除key为3的元素
m.erase(3);

//清空
m.erase(m.begin(),m.end());
m.clear();
printMap(m);

map容器查找和统计

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第12张图片

#include 

//查找和统计
void test01()
{
	map<int, int>m; 
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	//查找
	map<int, int>::iterator pos = m.find(3);

	if (pos != m.end())
	{
		cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}

	//统计
	int num = m.count(3);
	cout << "num = " << num << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

map容器排序

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第13张图片

#include 

// 自定义降序排序函数
class MyCompare {
public:
	bool operator()(int v1, int v2) {
		return v1 > v2;
	}
};

void test01() 
{
	//默认从小到大排序
	//利用仿函数实现从大到小排序
	map<int, int, MyCompare> m;

	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(5, 50));

	for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key:" << it->first << " value:" << it->second << endl;
	}
}

综合案例:员工分组

C++教程 | STL编程 | 常用函数操作手册 | set/multiset容器 | map/ multimap容器 | 综合案例:员工分组_第14张图片

#include
using namespace std;
#include 
#include 
#include 
#include 

/*
- 公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
- 员工信息有: 姓名  工资组成;部门分为:策划、美术、研发
- 随机给10名员工分配部门和工资
- 通过multimap进行信息的插入  key(部门编号) value(员工)
- 分部门显示员工信息
*/

#define CEHUA  0
#define MEISHU 1
#define YANFA  2

class Worker
{
public:
	string m_Name;  //员工姓名
	int m_Salary;  //员工薪资
};
// 将员工添加到vector中
void createWorker(vector<Worker>&v)
{
	string nameSeed = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		Worker worker;
		worker.m_Name = "员工";
		worker.m_Name += nameSeed[i];

		worker.m_Salary = rand() % 10000 + 10000; // 10000 ~ 19999
		//将员工放入到容器中
		v.push_back(worker);
	}
}

//员工分组
void setGroup(vector<Worker>&v,multimap<int,Worker>&m)
{   // 遍历员工容器
	for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
	{
		//产生随机部门编号
		int deptId = rand() % 3; // 0 1 2 

		//将员工随机插入到分组中
		//key部门编号,value具体员工
		m.insert(make_pair(deptId, *it));
	}
}

// 将multimap打印显示
void showWorkerByGourp(multimap<int,Worker>&m)
{
	// 0  A  B  C   1  D  E   2  F G ...
	cout << "策划部门:" << endl;
	
	// map只有find方法
	multimap<int,Worker>::iterator pos = m.find(CEHUA);
	int count = m.count(CEHUA); // 统计具体人数
	int index = 0;
	for (; pos != m.end() && index < count; pos++ , index++)
	{
		cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Salary << endl;
	}

	cout << "----------------------" << endl;
	cout << "美术部门: " << endl;
	pos = m.find(MEISHU);
	count = m.count(MEISHU); // 统计具体人数
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Salary << endl;
	}

	cout << "----------------------" << endl;
	cout << "研发部门: " << endl;
	pos = m.find(YANFA);
	count = m.count(YANFA); // 统计具体人数
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名: " << pos->second.m_Name << " 工资: " << pos->second.m_Salary << endl;
	}

}

int main() {
	// 随机数种子
	srand((unsigned int)time(NULL));

	//1、创建员工
	vector<Worker>vWorker;
	createWorker(vWorker);

	//2、员工分组
	multimap<int, Worker>mWorker;
	setGroup(vWorker, mWorker);


	//3、分组显示员工
	showWorkerByGourp(mWorker);

	测试
	//for (vector::iterator it = vWorker.begin(); it != vWorker.end(); it++)
	//{
	//	cout << "姓名: " << it->m_Name << " 工资: " << it->m_Salary << endl;
	//}

	system("pause");

	return 0;
}

你可能感兴趣的:(C/C++,c++,基础,STL)