Map容器
特点
1.map种所有元素都是pair
pair种第一个元素为key键值,起到索引作用,第二个元素为value实值
2.所有元素都会根据元素的值自动排序
map/multimap属于关联式容器,底层结构用二叉树实现
优点:
可以根据key值快速找到value值
map的构造和赋值
构造
map<T1,T2>mp; 默认构造
map(const map &mp); 拷贝构造
赋值:
map& operator=(const map &mp) 重载等号运算符
//map容器 构造和赋值
void test101()
{
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));
m.insert(pair<int, int>(4, 40));
printMap(m);
map<int, int>m1(m); //拷贝构造
printMap(m1);
map<int, int>m2 = m; //等号赋值
printMap(m2);
}
map大小和交换
功能描述:
统计map容器大小以及交换map容器
size() 返回容器中元素的数目
empty() 判断容器是否为空
swap() 交换两个集合容器
//map容器 大小和交换
void test102()
{
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));
m.insert(pair<int, int>(4, 40));
printMap(m);
if (m.empty())
{
cout << "m为空" << endl;
}
else
{
cout << "m不为空" << endl;
cout << "m的大小为:" << m.size() << endl;
}
map<int, int>m2;
m2.insert(pair<int, int>(1, 10));
m2.insert(pair<int, int>(2, 20));
m.swap(m2); //容器交换
printMap(m);
}
map容器插入和删除
map容器进行插入数据和删除数据
insert(elem) 在容器中插入数据
clear() 删除所有元素
erase(pos) 删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end) 删除区间beg,end的所有元素,返回下一个元素的迭代器
erase(key) 删除容器中值为key的元素
map 查找和统计
find(key) 查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回seet.end()
count(key) 统计key的元素个数
map容器的查找与删除等操作与set容器相似,此处不再赘述。
//map容器插入和删除
void test103()
{
map<int, int>m;
//插入方法1
m.insert(pair<int,int>(1,10));
//插入方法2
m.insert(make_pair(2, 20));
//插入方法3
m.insert(map<int, int>::value_type(3, 30));
//插入方法4
m[4] = 40;
//[]不建议插入,可以利用key访问value
printMap(m);
}
map容器排序
map容器默认排序规则为按照key值进行从小到大排序,掌握如何改变排序规则
主要技术点:
利用仿函数,可以改变排序规则
//仿函数
class MyCompare
{
public:
bool operator() (int v1, int v2)const
{
//降序
return v1 > v2;
}
};
//map容器排序
void test104()
{
map<int, int>m;
//插入方法1
m.insert(pair<int, int>(1, 10));
//插入方法2
m.insert(make_pair(2, 20));
//插入方法3
m.insert(map<int, int>::value_type(3, 30));
//插入方法4
m[4] = 40;
//[]不建议插入,可以利用key访问value
printMap(m);
map<int, int, MyCompare>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>(4, 40));
for (map<int, int, MyCompare>::iterator it = m1.begin(); it != m1.end(); it++)
{
cout << "key: " << it->first << "值:" << it->second << endl;
}
}
最后此处做一个
实例练习
案例:
公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在哪个部门工作
员工信息有:姓名 工资组成 部门分为:策划、美术、研发
随机给10名员工分配部门和工资
通过multimap进行信息的插入 key部门编号value员工
分部门显示员工信息
实现步骤
1.创建10名员工,放到vector中
2.遍历vector容器,取出每个员工,进行随机分组
3.分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器
4.分部门显示员工信息
实例代码
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<ctime>
using namespace std;
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class Worker
{
public:
string m_Name;
int m_Salary;
};
//创建员工
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;
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;
//将员工插入到分组中
//key部门编号.value具体员工
m.insert(make_pair(deptId, *it));
}
}
void showWorkerByGourp(multimap<int, Worker>& m)
{
cout << "策划部门" << endl;
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);
/*for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
{
cout << "姓名:" << it->m_Name << " 薪资:" << it->m_Salary << endl;
}*/
//2.员工分组
multimap<int, Worker>mWorker;
setGroup(vWorker, mWorker);
showWorkerByGourp(mWorker);
system("pause");
return 0;
}