27.C++之STL(五)

学习目标:

在这里插入图片描述


学习内容:

1. set/multiset容器相关知识

在这里插入图片描述

1.1 set/multiset构造函数和赋值

在这里插入图片描述

#include<iostream>
using namespace std;
#include<set>
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}
void test()
{
	set<int>s;

	//set容器中没有push来赋值,直接insert赋值
	s.insert(10);
	s.insert(30);
	s.insert(50);
	s.insert(20);
	s.insert(40);
	//set容器实现自动升序排列,不能插入相同的元素,是一个集合
	cout << "原始元素: ";
	printSet(s);

	//拷贝构造
	set<int>s1(s);
	cout << "拷贝构造: ";
	printSet(s1);

	//等号赋值
	set<int>s2;
	s2 = s;
	cout << "等号赋值: ";
	printSet(s2);

}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

1.2 set容器大小和交换

在这里插入图片描述

//set容器的大小和交换
#include<iostream>
using namespace std;
#include<set>
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}
void test()
{
	set<int>s;

	//set容器中没有push来赋值,直接insert赋值
	s.insert(10);
	s.insert(30);
	s.insert(50);
	s.insert(20);
	s.insert(40);
	//set容器实现自动升序排列,不能插入相同的元素,是一个集合
	cout << "原始元素: ";
	printSet(s);
	if (s.empty())
	{
		cout << "set容器为空!!" << endl;
	}
	else
	{
		cout << "set容器不为空!!" << endl;
		cout << "set容器的大小为:" << s.size() << endl;
	}

	set<int>s1;
	for (int i = 61; i < 66; i++)
	{
		s1.insert(i);
	}
	cout << "容器s1为: ";
	printSet(s1);

	cout << "交换后:-----------" << endl;
	s.swap(s1);
	printSet(s);
	printSet(s1);
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

1.3 set容器插入和删除

在这里插入图片描述

//插入和删除
#include<iostream>
using namespace std;
#include<set>
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test()
{
	set<int> s;

	//插入元素
	s.insert(20);
	s.insert(10);
	s.insert(40);
	s.insert(50);
	s.insert(30);
	cout << "原始元素:";
	printSet(s);

	//删除元素--erase(迭代器)
	s.erase(s.begin());
	cout << "删除最小的元素: ";
	printSet(s);

	//直接删除某一个元素
	s.erase(50);
	cout << "删除元素后: ";
	printSet(s);

	//删除整个区间元素-----相当于清除s.clear()
	s.erase(s.begin(), s.end());
	cout << "清除整个元素:";
	printSet(s);
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

1.4 set容器查找和统计

在这里插入图片描述

//查找和统计
#include<iostream>
using namespace std;
#include<set>

void printSet(set<int>&s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test()
{
	set<int>s;

	s.insert(10);
	s.insert(50);
	s.insert(30);
	s.insert(20);
	s.insert(40);

	cout << "原始元素:";
	printSet(s);

	//查找返回的是一个迭代器,必须标明类型
	set<int>::iterator pos = s.find(10);
	if (pos != s.end())
	{
		cout << "找到该元素!! " << endl;
		cout << "该元素出现的次数为:" << s.count(*pos) << endl;
	}
	else
	{
		cout << "未找到该元素!!" << endl;
	}
}

int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

1.5 set与multiset的区别

在这里插入图片描述

#include<iostream>
using namespace std;
#include<set>
void test()
{
	set<int>s;

	pair<set<int>::iterator,bool> ret = s.insert(10);
	if (ret.second)
	{
		cout << "第一次插入成功!!" << endl;
	}
	else
	{
		cout << "第一次插入失败!!" << endl;
	}

	pair<set<int>::iterator, bool> ret1 = s.insert(10);
	if (ret1.second)
	{
		cout << "第er次插入成功!!" << endl;
	}
	else
	{
		cout << "第er次插入失败!!" << endl;
	}
	
}

int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

1.6 pair队组的创建

在这里插入图片描述

//pair队组的创建
#include<iostream>
using namespace std;
void test()
{
	pair<string, int> p1("Jack", 18);
	cout << "姓名:" << p1.first << " " << "年龄:" << p1.second << endl;

	cout << "-----------------------------------------------------------" << endl;
	pair<string, int> p2 = make_pair("Tom", 20);
	cout << "姓名:" << p2.first << " " << "年龄:" << p2.second << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

1.7 内置类型排序规则

在这里插入图片描述

//内置数据类型的排序
#include<iostream>
using namespace std;
#include<set>
class compare
{
public:
	bool operator()(int a, int b)const //新版编译器要在此处加上一个const
	{
		return a > b;
	}
}; 
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{ 
		cout << *it << " ";
	}
	cout << endl;
}

void test()
{
	set<int> s;
	s.insert(10);
	s.insert(30);
	s.insert(50);
	s.insert(40);
	s.insert(20);

	cout << "默认为升序:";
	printSet(s);

	//要想降序,必须使用仿函数来声明,再插入数据,才能排序
	set<int,compare> s1;
	s1.insert(10);
	s1.insert(30);
	s1.insert(50);
	s1.insert(40);
	s1.insert(20);
	cout << "调整为降序:";
	//printSet(s1); 直接报错,必须重写
	for(set<int, compare>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

}

int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

1.8 自定义类型排序规则

//自定义数据类型的排序
#include<iostream>
#include<set>
#include<string>
using namespace std;

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

class Mycompare
{
public:
	bool operator()(const Person& p1, const Person& p2)const
	{
		return p1.m_Age > p2.m_Age;
	}
};
void test()
{
	set<Person, Mycompare>s;
	//使用set打印多个对象时,必须指定排序规则

	Person p1("刘备", 28);
	Person p2("关羽", 27);
	Person p3("张飞", 26);
	Person p4("赵云", 24);
	Person p5("曹操", 38);
	Person p6("孙权", 35);

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	s.insert(p5);
	s.insert(p6);

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

}

int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

2. map/multimap容器相关知识

在这里插入图片描述

2.1 map/multimap构造函数和赋值

在这里插入图片描述

#include<iostream>
#include<map>
#include<string>
using namespace std;
void printMap(map<string, int>& m)
{
	for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "姓名:" << (*it).first << " 年龄: " << (*it).second << endl;
	}
	cout << endl;
}

void test()
{
	map<string, int> m;

	m.insert(pair<string, int>("刘备", 28));
	m.insert(pair<string, int>("关羽", 27));
	m.insert(pair<string, int>("张飞", 26));
	m.insert(pair<string, int>("赵云", 24));

	printMap(m);

	//拷贝构造
	map<string, int>m1(m);
	printMap(m1);

	//赋值
	map<string, int>m2;
	m2 = m;
	printMap(m2);
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

2.2 map/multimap大小和交换

在这里插入图片描述

//大小和交换
#include<iostream>
#include<map>
#include<string>
using namespace std;
void printMap(map<string, int>& m)
{
	for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "姓名:" << (*it).first << " 年龄: " << (*it).second << endl;
	}
	cout << endl;
}
void test()
{
	map<string, int> m;

	m.insert(pair<string, int>("刘备", 28));
	m.insert(pair<string, int>("关羽", 27));
	m.insert(pair<string, int>("张飞", 26));
	m.insert(pair<string, int>("赵云", 24));

	if (m.empty())
	{
		cout << "map容器为空!!!" << endl;
	}
	else
	{
		cout << "map容器不为空!!" << endl;
		cout << "map容器的大小为:" << m.size() << endl;
	}

	//交换
	map<string, int>m1;
	m1.insert(pair<string,int>("小乔", 18));
	m1.insert(pair < string, int>("大乔", 18));
	m1.insert(pair < string, int>("貂蝉", 18));
	m1.insert(pair < string, int>("孙尚香", 18));

	cout << "交换前:" << endl;
	printMap(m);
	printMap(m1);

	cout << "交换后:" << endl;
	m.swap(m1);
	printMap(m);
	printMap(m1);
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

2.3 map/multimap插入和删除

在这里插入图片描述

//插入和删除
#include<iostream>
#include<map>
#include<string>
using namespace std;
void printMap(map<string, int>& m)
{
	for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "姓名:" << (*it).first << " 年龄: " << (*it).second << endl;
	}
	cout << endl;
}

void test()
{
	map<string, int> m;

	//第一种插入
	m.insert(pair<string, int>("刘备", 28));
	//第二种插入
	m.insert(make_pair("关羽", 27));
	//第三种插入
	m.insert(map<string, int>::value_type("张飞",26));
	//第四种插入
	m["赵云"] = 24;
	printMap(m);

	//删除
	m.erase("刘备");
	printMap(m);

	m.erase(m.begin());
	printMap(m);

	//区间删除相当于清除m.clear()
	m.erase(m.begin(), m.end());
	printMap(m);
}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

2.4 map/multimap查找和统计

在这里插入图片描述

//统计和查找
#include<iostream>
#include<map>
using namespace std;
void test()
{
	map<int, int>m;

	m.insert(make_pair(1, 100));
	m.insert(make_pair(2, 200));
	m.insert(make_pair(3, 300));

	map<int, int>::iterator pos = m.find(3);
	if (pos != m.end())
	{
		cout << "找到该元素!!" << endl;
		cout << ( * pos).second << endl;
		cout << m.count(( * pos).first) << endl;
	}
	else
	{
		cout << "未找到该元素!!" << endl;
	}

}
int main()
{
	test();
	system("pause");
	return 0;
}

在这里插入图片描述

2.5 map/multimap排序

在这里插入图片描述

//排序
#include<iostream>
#include<map>
using namespace std;
class mycompare
{
public:
	bool operator()(int a, int b)const
	{
		return a > b;
	}
};
void test()
{
	map<int, int, mycompare>m;

	m.insert(make_pair(1, 100));
	m.insert(make_pair(2, 200));
	m.insert(make_pair(3, 300));

	for (map<int, int, mycompare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << (*it).first << " *-* " << (*it).second << endl;
	}
	cout << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

2.6 map/multimap案例应用

在这里插入图片描述

#include<iostream>
#include<vector>
#include<string>
#include<map>
#define cehua 0
#define meishu 1
#define yanfa 2
using namespace std;
class Workers
{
public:
	Workers(string name, int salary)
	{
		this->m_Name = name;
		this->m_Salary = salary;
	}
	string m_Name;
	int m_Salary;
};
void creatWorkers(vector<Workers>& v)
{
	string nameSeed = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		string name = "员工";
		name += nameSeed[i];
		int salary = rand() % 10000 + 10000;
		Workers w(name, salary);
		v.push_back(w);
	}
}
void setGroup(vector<Workers>& vW, multimap<int, Workers>& mW)
{
	for (vector<Workers>::iterator it = vW.begin(); it != vW.end(); it++)
	{
		//随机产生部门号
		int deptId = rand() % 3;
		//key值为部门号,Workers为员工信息
		mW.insert(pair<int, Workers>(deptId, *it));
	}
}

void showWorkers(multimap<int, Workers>& mWorkers)
{
	cout << "策划部门: " << endl;
	multimap<int, Workers>::iterator pos = mWorkers.find(cehua);
	//统计每个部门的人数
	int num = mWorkers.count(cehua);
	int index = 0;
	for (; pos != mWorkers.end()&&index<num ; pos++,index++)
	{
		cout << "姓名:" << (*pos).second.m_Name << " 工资:" << (*pos).second.m_Salary << endl;
	}

	cout << "------------------------------------------------" << endl;
	cout << "美术部门: " << endl;
	multimap<int, Workers>::iterator pos1 = mWorkers.find(meishu);
	//统计每个部门的人数
	int num1 = mWorkers.count(meishu);
	int index1 = 0;
	for (; pos1 != mWorkers.end() && index1 < num1; pos1++, index1++)
	{
		cout << "姓名:" << (*pos1).second.m_Name << " 工资:" << (*pos1).second.m_Salary << endl;
	}

	cout << "------------------------------------------------" << endl;
	cout << "研发部门: " << endl;
	multimap<int, Workers>::iterator pos2 = mWorkers.find(yanfa);
	//统计每个部门的人数
	int num2 = mWorkers.count(yanfa);
	int index2= 0;
	for (; pos2 != mWorkers.end() && index2 < num2; pos2++, index2++)
	{
		cout << "姓名:" << (*pos2).second.m_Name << " 工资:" << (*pos2).second.m_Salary << endl;
	}
}
int main()
{
	srand((unsigned int)time(NULL));
	//1.创建员工
	vector<Workers>vWorkers;
	creatWorkers(vWorkers);

	//测试
	/*for (vector<Workers>::iterator it = vWorkers.begin(); it != vWorkers.end(); it++)
	{
		cout << (*it).m_Name << "  " << (*it).m_Salary << endl;
	}*/

	//2.员工分组
	multimap<int, Workers>mWorkers;
	setGroup(vWorkers,mWorkers);

	//测试
	/*for (multimap<int, Workers>::iterator it = mWorkers.begin(); it != mWorkers.end(); it++)
	{
		cout << "编号部门:" << (*it).first << " 员工姓名:" << (*it).second.m_Name << " 工资:" << (*it).second.m_Salary << endl;
	}*/

	//3.显示员工信息
	showWorkers(mWorkers);


	system("pause");
	return 0;
}

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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值