map容器使用及员工分组实例

一、map容器基本概念

  • map中所有元素都是pair对组
  • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
  • 所有元素都会根据元素键值自动由小到大排序
  • map/multimap属于关联式容器,底层结构是用二叉树实现
    优点
  • 可以根据key值快速找到value值
  • map和multimap的区别
  • map不允许容器中有重复key值
  • multimap允许有重复key值

二、map容器的构造和赋值

#include<iostream>
#include<map>
using namespace std;
void printMap(map<int, int>&m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
		cout << "key=" << it->first << ",value=" << it->second << endl;
	cout << endl;
}
void test01()
{
	map<int, int>m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(3, 20));
	m1.insert(pair<int, int>(2, 30));
	m1.insert(pair<int, int>(4, 40));
	printMap(m1);
	map<int, int>m2(m1);
	printMap(m2);
	map<int, int>m3;
	m3 = m2;
	printMap(m3);
}

map也采用insert()添加值,对对组要指明两个参数的类型,first代表键值key,second则是存的value值

三、map容器的大小及交换

void test02()
{
	map<int, int>m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(2, 20));
	m1.insert(pair<int, int>(3, 30));
	if (m1.empty())
		cout << "m为空" << endl;
	else
	{
		cout << "m不为空" << endl;
		cout << "m的大小为:" << m1.size() << endl;
	}
	map<int, int>m2;
	m2.insert(pair<int, int>(4, 37));
	m2.insert(pair<int, int>(5, 73));
	m2.insert(pair<int, int>(6, 373));
	cout <<"交换前:" << endl;
	printMap(m1);
	printMap(m2);
	m1.swap(m2);
	cout << "交换后:" << endl;
	printMap(m1);
	printMap(m2);
}

size返回数据个数,使用swap交换

四、map的插入和删除

void test03()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(map<int, int>::value_type(3, 30));
	m[4] = 40;
	cout << m[5] << endl;
	printMap(m);
	m.erase(m.begin());
	printMap(m);
	m.erase(3);
	printMap(m);
	m.clear();
}

map插入数据有四种方式

  • insert指明pair对组插值
  • insert使用make_pair插值
  • insert使用map下的==value_type插值
  • 使用[ ]插值

建议使用第二种,比较简单方便,第四种虽然简单,但在实际应用中容易误改数据值或是被当作数组难以分清
删除数据使用erae函数,可传迭代器删除,也可传值删除,clear清除函数就是erase从头到尾。
对m[5]未赋值打印可以看出,map默认value值为0,
且打印也是按键值顺序打印的
在这里插入图片描述

五、map的查找与统计

void test04()
{
	map<int, int>m1;
	m1.insert(pair<int, int>(1, 10));
	m1.insert(pair<int, int>(2, 20));
	m1.insert(pair<int, int>(3, 30));
	m1.insert(pair<int, int>(3, 40));
	if (m1.find(3) != m1.end())
		cout << "找到了 key=" << m1.find(3)->first << ",value=" << m1.find(3)->second << endl;
	else
		cout << "没找到" << endl;
	int num = m1.count(3);
	cout << "num=" << num << endl;
}

使用find函数查找,参数是value值,
计数函数count再map里只能返回0或一,因为键值不能重复,所以只能找到一个对应的或找不到

六、map容器排序

class cmp
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};
void test05()
{
	map<int, int,cmp>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,cmp>::iterator it=m.begin();it!=m.end();it++)
		cout << "key=" << it->first << ",value=" << it->second << endl;
}

map默认升序排序,想要实现其他类型排序,需要自定义比较类,类中重载括号(),即仿函数,来自定义比较方式。创建map容器对象时,需要传入类名。
在这里插入图片描述

七、员工分组实例

  • 将十名员工(ABCDEFGHIJ)分派到不同部门工作
  • 员工信息有:姓名 、工资、部门(策划、美术、研发)
  • 利用rand()随机给10人分配工资
  • 使用multimap插入信息 key(部门编号) value(员工)
  • 分部门显示员工信息
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<time.h>
using namespace std;
#define 策划 0 //宏定义部门编号
#define 美术 1
#define 研发 2
class worker
{
public:
	string m_name;
	int m_salary;
};//员工类
void creatWorker(vector<worker>&v)
{
	string namseed = "ABCDEFGHIJ";//名称种子
	for (int i = 0; i < 10; i++)
	{
		worker p;
		p.m_name = "员工";
		p.m_name += namseed[i];
		p.m_salary = rand() % 10000 + 10000;//工资10000~19999
		v.push_back(p);//存入vector容器中
	}
}
void setgroup(vector<worker>&v, multimap<int, worker>&m)
{
	for (vector<worker>::iterator i = v.begin(); i != v.end(); i++)
	{
		int rid = rand() % 3;//随机生成部门编号
		m.insert(make_pair(rid, *i));//将编号以及vector容器中存的员工信息插入到map容器中
	}
}
void showworker(multimap<int, worker>&m)
{
	cout << "策划部门:" << endl;
	multimap<int,worker>::iterator pos=m.find(策划);利用find查找key值,即部门
	int count = m.count(策划);//统计部门人数
	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(美术);
	count = m.count(美术);
	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(研发);
	count = m.count(研发);
	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));//设置时间戳生成真实随机数
	vector<worker>vp;
	creatWorker(vp);
	multimap<int, worker>mp;
	setgroup(vp,mp);
	showworker(mp);
	return 0;
}

在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

...404 Not Found

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值