1.map基本概念
- map中所有元素都是pair
- pair中第一个元素为key,起到索引作用,第二个元素为value
- 所有元素都会根据元素的键值自动排序
本质:
- map/multimap属于关联式容器,底层结构是用二叉树实现
优点:
- 可以根据key值快速找到value值
map和multimap的区别:
- map不允许容器中有重复key值元素
- multimap允许容器中有重复key值元素
2.map构造和赋值
打印Map:
void printMap(map<int,int> &m)
{
for(map<int,int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << (*it).first << " " << it->second << endl;
}
}
构造:
void test01()
{
map<int, int> m; // <索引,实值> 插入时会按照key值进行排序
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
printMap(m);
// 拷贝构造
map<int,int> m2(m);
// 赋值
map<int, int> m3;
m3 = m;
}
3.map大小和交换
- size();
- empty();
- swap(st);
4.map插入和删除
- insert(elem); 在容器中插入元素
- clear(); 清除所有元素
- erase(pos); 删除pos迭代器所指的元素,返回下一个元素的迭代器
- erase(beg, end); 删除[beg, end)的所有元素,返回下一个元素的迭代器
- erase(elem); 删除容器中值为elem的元素
void test01()
{
map<int, int> m;
m.insert(pair<int, int>(1, 10));
m.insert(make_pair(2, 10));
m.insert(map<int, int>::value_type(3, 30));
// 不建议以[]方式插入元素,这种方式主要用来利用key访问value
m[4] = 40;
// 删除
m.erase(m.begin());
m.erase(3); // 按照key删除!!!
m.erase(m.begin(), m.end());
m.clear();
}
5.map查找和统计
void test01()
{
// 查找
map<int, int> m;
m.insert(pair<int,int>(1, 10));
m.insert(pair<int,int>(3, 30));
m.insert(pair<int,int>(2, 20));
m.insert(pair<int,int>(4, 40));
map<int,int>::iterator pos = m.find(3);
if(pos != m.end())
{
cout << "查到了元素 key=" << (*pos).first << " value=" << (*pos).second << endl;
}
else
{
cout << "未找到元素" << endl;
}
// 统计
// map不允许插入重复key元素,count统计而言,结果要么为0,要么为1
// multimap的count统计结果可能大于1
cout << m.count(3) << endl;
}
6.map容器排序
- map默认排序规则为:按照key值从小到大排序
- 利用仿函数,可以降序排列
class MyCompare
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test01()
{
// 查找
map<int, int, MyCompare> m;
m.insert(pair<int,int>(3, 30));
m.insert(pair<int,int>(3, 30));
m.insert(pair<int,int>(2, 20));
m.insert(pair<int,int>(4, 40));
m.insert(pair<int,int>(5, 50));
for(map<int,int, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
{
cout << (*it).first << " " << it->second << endl;
}
}
注意:自定义数据类型不能用(int &v1, int &v2),只有内置数据类型才可以。而且对于自定义的数据类型,需要指定排序规则。
7.案例-员工分组
- 公司今天招聘了10名员工,需要指派每个员工在哪个部门工作
- 员工信息由姓名、工资组成,部门分为策划、美术、研发
- 随机给10名员工分配部门和工资
- 通过multimap进行信息的插入 key(部门编号) value(员工)
- 分部门显示员工信息
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class Person
{
public:
string m_name;
int m_money;
};
void creatPerson(vector<Person> &v)
{
string name_seed = "ABCDEFGHIJ";
for(int i=0; i<10; i++)
{
Person p;
p.m_name = "Worker";
p.m_name += name_seed[i];
p.m_money = rand() % 10000 + 10000;
v.push_back(p);
}
}
void setGroup(vector<Person> &v, multimap<int, Person> &m)
{
for(vector<Person>::iterator it=v.begin(); it!=v.end(); it++)
{
// 随机部门编号
int deptId = rand() % 3; // 0,1,2
m.insert(make_pair(deptId, *it));
}
}
void showWorker(multimap<int,Person> &m)
{
cout << "策划部门:" << endl;
multimap<int,Person>::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_money << endl;
}
cout << "-----------------" << endl;
cout << "美术部门:" << endl;
multimap<int,Person>::iterator pos2 = m.find(MEISHU);
int count2 = m.count(MEISHU);
int index2 = 0;
for(; pos2 != m.end() && index2 < count2; pos2++, index2++)
{
cout << pos2->second.m_name << " " << pos2->second.m_money << endl;
}
cout << "-----------------" << endl;
cout << "研发部门:" << endl;
multimap<int,Person>::iterator pos3 = m.find(YANFA);
int count3 = m.count(YANFA);
int index3 = 0;
for(; pos3 != m.end() && index3 < count3; pos3++, index3++)
{
cout << pos3->second.m_name << " " << pos3->second.m_money << endl;
}
}
void test01()
{
srand((unsigned int)time(NULL));
vector<Person> v;
creatPerson(v);
multimap<int, Person> m;
setGroup(v, m);
// 分组显示员工
showWorker(m);
}