目录
map/ multimap容器
map基本概念
简介:
-
map中所有元素都是pair
-
pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)想想list的结构
-
所有元素都会根据元素的键值自动排序
本质:
-
map/multimap属于关联式容器,底层结构是用二叉树实现。
优点:
-
可以根据key值快速找到value值
map和multimap区别:
-
map不允许容器中有重复key值元素
-
multimap允许容器中有重复key值元素(会议一下muiliset,muili是升级的意思?如果说map是根据key索引的,那为什么muiliimap允许key重复?编译器自动给key值加个尾巴?)
map构造和赋值
功能描述:
-
对map容器进行构造和赋值操作
函数原型:
构造:
-
map<T1, T2> mp;
//map默认构造函数: -
map(const map &mp);
//拷贝构造函数
赋值:
-
map& operator=(const map &mp);
//重载等号操作符
using namespace std;
void print_map(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容器
map<int, int>m;//键值key 实存数据
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));
print_map(m);
//拷贝构造
map<int, int>m2(m);
//赋值构造
map<int,int>m3;
m3 = m;
}
int main() {
test01();
system("pause");
return 0;
}
map大小和交换
**功能描述:**
* 统计map容器大小以及交换map容器
函数原型:
- `size();` //返回容器中元素的数目
- `empty();` //判断容器是否为空
- `swap(st);` //交换两个集合容器
让人惊讶的是print函数这里和其他的不一样
using namespace std;
void print_map(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容器
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 << "m1为空" << endl;
}
else
{
cout << "m1不为空,其大小为:" <<m1.size() << endl;
}
//用来交换
map<int, int>m2;
m2.insert(pair<int, int>(4, 400));
m2.insert(pair<int, int>(5, 500));
m2.insert(pair<int, int>(6, 600));
//交换前
cout << "交换前" << endl;
print_map(m1);
print_map(m2);
m1.swap(m2);
cout << "交换后" << endl;
print_map(m1);
print_map(m2);
}
int main() {
test01();
system("pause");
return 0;
}
map插入和删除
功能描述:
-
map容器进行插入数据和删除数据
函数原型:
-
insert(elem);
//在容器中插入元素。 -
clear();
//清除所有元素 -
erase(pos);
//删除pos迭代器所指的元素,返回下一个元素的迭代器。 -
erase(beg, end);
//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。 -
erase(key);
//删除容器中值为key的元素。
using namespace std;
void print_map(map<int, int>m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key值:" << it->first << "实质 " << it->second << endl;
}
cout << endl;
}
void test01()
{
//创建map容器
map<int, int>m1;
//插入
//第一种 正规
m1.insert(pair<int, int>(1, 10));
//第二种 推荐
m1.insert(make_pair(2, 20));
//第三种 太长
m1.insert(map<int, int>::value_type(3, 30));
//第四种 易报错
m1[4] = 40;
//容易查错,查错就会创建一个空的
print_map(m1);
//删除
m1.erase(m1.begin());
print_map(m1);
m1.erase(3);//索引到key为三删除
print_map(m1);
//清空
m1.erase(m1.begin(), m1.end());
//同样,clear也可以做到
m1.clear();
print_map(m1);
}
int main() {
test01();
system("pause");
return 0;
}
-
map插入方式很多,记住其一即可
-
插入 --- insert
-
删除 --- erase
-
清空 --- clear
map查找和统计
功能描述:
-
对map容器进行查找数据以及统计数据
函数原型:
-
find(key);
//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end(); -
count(key);
//统计key的元素个数
using namespace std;
void print_map(map<int, int>m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key值:" << it->first << "实质 " << it->second << endl;
}
cout << endl;
}
void test01()
{
//查找
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));
//创建一个迭代器去接受
map<int, int>::iterator pos = m1.find(3);
if (pos != m1.end())
{
cout << "查到了元素key=" << (*pos).first << "value=" << pos->second << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
int num = m1.count(3);
cout << "num=" << num << endl;
}
总结:
-
查找 --- find (返回的是迭代器)
-
统计 --- count (对于map,结果为0或者1)
map容器排序
学习目标:
-
map容器默认排序规则为 按照key值进行 从小到大排序,掌握如何改变排序规则
主要技术点:
-
利用仿函数,可以改变排序规则
#include<iostream>
#include<string>
#include<map>
using namespace std;
class my_compare//降序排序
{
public:
bool operator()(int v1,int v2) const//更新后数据丢失,加上const限制更改
{
return v1 > v2;//为真正常输出,为假的话反过来
}
};
void print_map(map<int, int,my_compare>m1)
{
for (map<int, int, my_compare>::iterator it = m1.begin(); it != m1.end(); it++)
{
cout << "key值:" << it->first << "实质 " << it->second << endl;
}
cout << endl;
}
void test01()
{
map<int, int,my_compare>m1;
m1.insert(pair<int, int>(1, 10));
m1.insert(pair<int, int>(2, 20));
m1.insert(pair<int, int>(4, 30));
m1.insert(pair<int, int>(3, 80));
m1.insert(pair<int, int>(6, 60));
m1.insert(pair<int, int>(5, 40));
m1.insert(pair<int, int>(7, 90));
//排序后
print_map(m1);
}
int main() {
test01();
system("pause");
return 0;
}
案例-员工分组
案例描述
-
公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
-
员工信息有: 姓名 工资组成;部门分为:策划、美术、研发
-
随机给10名员工分配部门和工资
-
通过multimap进行信息的插入 key(部门编号) value(员工)
-
分部门显示员工信息
实现步骤
-
创建10名员工,放到vector中
-
遍历vector容器,取出每个员工,进行随机分组
-
分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
-
分部门显示员工信息
创建10名员工,放到vector中
#include<iostream>
using namespace std;
#include <vector>
#include <string>
#include <map>
#include <ctime>
class worker
//创建class类,创建员工属性
{
public:
string wokr_name;
int work_id;
};
//一共两个,一个int ,一个string
void create_worker(vector<worker>&v)
{
string name_seed = "ABCDEFGHIJ";
//随机数种子
for (int i = 0; i < 10; i++)
{
worker m_worker;
m_worker.wokr_name = "员工";
m_worker.wokr_name += name_seed[i];
//让string和种子库中abcd等相加
m_worker.work_id = rand() % 10000 + 10000;
//令一个员工id等于随机数
v.push_back(m_worker);
//把它查到数组里去
}//for循环让十个员工都有了值,更改i就可以增添人数.
}
void test01()
{
//1.创建员工
vector<worker>vc_worker;
//使用vector创建第一个员工容器
create_worker(vc_worker);
//给这个员工容器赋值内容,姓名和id
}
int main()
{
test01();
system("pause");
return 0;
}
遍历vector容器,取出每个员工,进行随机分组
其他的一致,就是测试代码是否成功
#include<iostream>
using namespace std;
#include <vector>
#include <string>
#include <map>
#include <ctime>
class worker
//创建class类,创建员工属性
{
public:
string wokr_name;
int work_id;
};
//一共两个,一个int ,一个string
void create_worker(vector<worker>&v)
{
string name_seed = "ABCDEFGHIJ";
//随机数种子
for (int i = 0; i < 10; i++)
{
worker m_worker;
m_worker.wokr_name = "员工";
m_worker.wokr_name += name_seed[i];
//让string和种子库中abcd等相加
m_worker.work_id = rand() % 10000 + 10000;
//令一个员工id等于随机数
v.push_back(m_worker);
//把它查到数组里去
}//for循环让十个员工都有了值,更改i就可以增添人数.
}
void test01()
{
//1.创建员工
vector<worker>vc_worker;
//使用vector创建第一个员工容器
create_worker(vc_worker);
//给这个员工容器赋值内容,姓名和id
//测试代码
for (vector<worker>::iterator it = vc_worker.begin(); it != vc_worker.end(); it++)
{
cout << "姓名: " << it->wokr_name << " id: " << it->work_id << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
#include<iostream>
using namespace std;
#include <vector>
#include <string>
#include <map>
#include <ctime>
class worker
//创建class类,创建员工属性
{
public:
string wokr_name;
int work_id;
};
//一共两个,一个int ,一个string
void create_worker(vector<worker>&v)
{
string name_seed = "ABCDEFGHIJ";
//随机数种子
for (int i = 0; i < 10; i++)
{
worker m_worker;
m_worker.wokr_name = "员工";
m_worker.wokr_name += name_seed[i];
//让string和种子库中abcd等相加
m_worker.work_id = rand() % 10000 + 10000;
//令一个员工id等于随机数
v.push_back(m_worker);
//把它查到数组里去
}//for循环让十个员工都有了值,更改i就可以增添人数.
}
//员工分组
void set_group(vector<worker>&v,multimap<int,worker>&m)
//引入的两个容器,一个是vector,这是因为要给每一个员工加上部门号
//multimap的是用来给worker加上部门编号,key就是部门编号
{
for (vector<worker>::iterator it = v.begin(); it != v.end(); it++)
//for循环就是让每一个员工都插入一个部门编号
{
//产生一个随机部门的编号
int dept_id = rand() % 3;
//将员工插入分组之中
//key代表部门编号,value代表具体员工
m.insert(make_pair(dept_id, *it));
}
}
void test01()
{
//1.创建员工
vector<worker>vc_worker;
//使用vector创建第一个员工容器
create_worker(vc_worker);
//2、员工分组
multimap<int, worker>shiji_worker;
//使用multimap创建容器,前面是key,后边是worker
//key可以当做部门号来索引
set_group(vc_worker, shiji_worker);
}
int main()
{
test01();
system("pause");
return 0;
}
分部门显示员工信息
#include<iostream>
using namespace std;
#include <vector>
#include <string>
#include <map>
#include <ctime>
#define cehua 0
#define meishu 1
#define yanfa 2
class worker
{
public:
string wokr_name;
int work_id;
};
void create_worker(vector<worker>&v)
{
string name_seed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
worker m_worker;
m_worker.wokr_name = "员工";
m_worker.wokr_name += name_seed[i];
m_worker.work_id = rand() % 10000 + 10000;
v.push_back(m_worker);
}
}
//员工分组
void set_group(vector<worker>&v,multimap<int,worker>&m)
{
for (vector<worker>::iterator it = v.begin(); it != v.end(); it++)
{
//产生一个随机部门的编号
int dept_id = rand() % 3;
//将员工插入分组之中
//key代表部门编号,value代表具体员工
m.insert(make_pair(dept_id, *it));
}
}
void show_worker_group(multimap<int, worker>& m)
{
// 0 A B C 1 D E 2 F G ...
cout << "策划部门打印 " << endl;
multimap<int,worker>::iterator pos= m.find(cehua);//找到部门编号
int count = m.count(cehua); //统计策划的具体人数
int index = 0; //引入一个变量,只要index小于count,就说明还有没打印完的
for (; pos != m.end() && index < count; pos++, index++)
//这里的pos是省略写法
{
cout << "姓名: " << pos->second.wokr_name << " id: " << pos->second.work_id << endl;
}
cout << "----------------------" << endl;
cout << "美术部门打印 " << endl;
pos = m.find(meishu);
count = m.count(meishu);//统计策划的具体人数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
//这里的pos是省略写法
{
cout << "姓名: " << pos->second.wokr_name << " id: " << pos->second.work_id << endl;
}
cout << "----------------------" << endl;
cout << "研发部门打印 " << endl;
pos = m.find(yanfa);
count = m.count(yanfa);//统计策划的具体人数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
//这里的pos是省略写法
{
cout << "姓名: " << pos->second.wokr_name << " id: " << pos->second.work_id << endl;
}
}
void test01()
{
srand((unsigned int)time(NULL));
//1.创建员工
vector<worker>vc_worker;
create_worker(vc_worker);
//测试代码
//for (vector<worker>::iterator it = vc_worker.begin(); it != vc_worker.end(); it++)
//{
// cout << "姓名: " << it->wokr_name << " id: " << it->work_id << endl;
//}
//2、员工分组
multimap<int, worker>shiji_worker;
set_group(vc_worker, shiji_worker);
//分组显示员工
show_worker_group(shiji_worker);
}
int main()
{
test01();
system("pause");
return 0;
}