目录
map/ multimap容器
map构造和赋值
map大小和交换
map插入和删除
map查找和统计
map容器排序
案例-员工分组
实现步骤
分部门显示员工信息
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
mp; //map默认构造函数: -
map(const map &mp); //拷贝构造函数
赋值:
-
map& operator=(const map &mp); //重载等号操作符
using namespace std; void print_map(map& m) { for (map ::iterator it = m.begin(); it != m.end(); it++) { cout << "key值= " << (*it).first << " value= " << it->second << endl; } cout << endl; } void test01() { //创建map容器 map m;//键值key 实存数据 m.insert(pair (1, 10)); m.insert(pair (2, 20)); m.insert(pair (3, 30)); m.insert(pair (4, 40)); print_map(m); //拷贝构造 map m2(m); //赋值构造 map 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& m) { for (map ::iterator it = m.begin(); it != m.end(); it++) { cout << "key值= " << (*it).first << " value= " << it->second << endl; } cout << endl; } void test01() { //创建map容器 map m1; m1.insert(pair (1, 10)); m1.insert(pair (2, 20)); m1.insert(pair (3, 30)); if (m1.empty())//用来检验是否为空 { cout << "m1为空" << endl; } else { cout << "m1不为空,其大小为:" < m2; m2.insert(pair (4, 400)); m2.insert(pair (5, 500)); m2.insert(pair (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(mapm) { for (map ::iterator it = m.begin(); it != m.end(); it++) { cout << "key值:" << it->first << "实质 " << it->second << endl; } cout << endl; } void test01() { //创建map容器 map m1; //插入 //第一种 正规 m1.insert(pair (1, 10)); //第二种 推荐 m1.insert(make_pair(2, 20)); //第三种 太长 m1.insert(map ::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(mapm) { for (map ::iterator it = m.begin(); it != m.end(); it++) { cout << "key值:" << it->first << "实质 " << it->second << endl; } cout << endl; } void test01() { //查找 map m1; m1.insert(pair (1, 10)); m1.insert(pair (2, 20)); m1.insert(pair (3, 30)); //创建一个迭代器去接受 map ::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#include #include
案例-员工分组
案例描述
-
公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
-
员工信息有: 姓名 工资组成;部门分为:策划、美术、研发
-
随机给10名员工分配部门和工资
-
通过multimap进行信息的插入 key(部门编号) value(员工)
-
分部门显示员工信息
实现步骤
-
创建10名员工,放到vector中
-
遍历vector容器,取出每个员工,进行随机分组
-
分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
-
分部门显示员工信息
创建10名员工,放到vector中
遍历vector容器,取出每个员工,进行随机分组
分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
分部门显示员工信息
创建10名员工,放到vector中
#includeusing namespace std; #include #include #include
遍历vector容器,取出每个员工,进行随机分组
其他的一致,就是测试代码是否成功
#includeusing namespace std; #include #include #include
分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
#includeusing namespace std; #include #include #include
分部门显示员工信息
#include
using namespace std;
#include
#include
#include



