三. STL常用容器
9. map / multimap容器
9.1 map基本概念
- map中所有元素都是pair ;
- pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值);
- 所有元素会根据元素的键值自动排序。
本质:map/multimap属于关联式容器,底层结构是用二叉树实现。
优点:可以根据key值快速找到value值。
map 和 multimap 的区别:
- map不允许容器中有重复key值元素;
- multimap允许容器中有重复key值元素。
9.2 map构造和赋值
map<int,int>m; //默认构造
m.insert(pair<int,int>(1,10));
map<int,int>m2(m); //拷贝构造
map<int,int>m3;
m3=m2; //赋值
总结:map中所有元素都是成对出现,插入数据时要使用对组。
9.3 map 大小和交换
m.size();
m.empty();
m.swap(m2);
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;
}
9.4 map 插入和删除
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;
m.erase(m.begin());
m.erase(3);
m.clear();
9.5 map 查找和统计
map<int,int>::iterator pos =m.find(3);
if(pos!=m.end())
{
cout<<"找到了元素key ="<<(*pos).first
<<"value ="<<(*pos).second<<endl;
}
int num =m.count(3);
//统计--count (对于map,结果为0或1)
9.6 map 容器排序
- 利用仿函数可以指定map容器的排序规则
- 对于自定义数据类型,map必须指定排序规则,同set容器
10. 案例 - 员工分组
10.1 案例描述
- 公司招聘了10个员工(ABCDEFGHI),10名员工进入公司之后,需要指派员工在哪个部门工作
- 员工信息有:姓名,工资组成;部门分为:策划,美术,研发
- 随机给10名员工分配部门和工资
- 通过multimap进行信息的插入key(部门编号)value(员工)
- 分部门显示员工信息
10.2 案例描述
- 创建10名员工,放到vector中;
- 遍历vector容器,取出每个员工,进行随机分组;
- 分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中;
- 分部门显示员工信息。
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<ctime>
using namespace std;
/*
公司今天招聘了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;
};
//创建员工函数
void creatworker(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;
//将员工放入到容器中
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 detID = rand() % 3;
//将员工插入到分组中
//key部门编号,value具体员工
m.insert(make_pair(detID,(*it)));
}
}
//显示员工函数
void showgroup(multimap<int, Worker>& m)
{
cout << "策划部门:" << endl;
multimap<int, Worker>::iterator pos = m.find(cehua);
int count = m.count(cehua);//统计策划部门具体的人数
for (int index =0; 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);//统计策划部门具体的人数
for (int index = 0; 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(cehua);//统计策划部门具体的人数
for (int index = 0; 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>v;
creatworker(v);
//测试
/*for (vector<Worker>::iterator it = v.begin();
it != v.end(); it++)*/
/*{
//cout << "姓名:" << it->m_Name << " 工资:"
<< it->m_Salary << endl;
}*/
//2.员工分组
multimap<int, Worker>m;
setgroup(v, m);
//3.分组显示员工
showgroup(m);
system("pause");
return 0;
}










