STL常用容器 目录:
1. string容器
2. vector容器
3. deque容器
4. stack容器
5. queue容器
6. list容器
7. set/multiset容器
8. map/multimap容器
8.1 map基本概念
简介:
- map中所有元素都是pair
- pair中第一个元素为key(键值),起索引作用;第二个元素为value(实值)
- 所有元素都会根据元素的键值自动排序
本质:
- map/multimap属于关联式容器,底层结构是用二叉树实现
优点:
- 可以根据key值快速找到value值
map和multimap区别:
- map不允许容器中有重复key值元素
- multimap允许容器中有重复key值元素
8.2 map构造和赋值
功能描述:
- 对map容器进行构造以及赋值操作
函数原型:
map(T1, T2) mp;
//默认构造函数map(const map &mp);
//拷贝构造函数map& operator=(const map& mp);
//赋值:重载等号操作符
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;
}
void test01()
{
//创建map容器
map<int, int>m;
//插入(必须以对组的方式)
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(4, 40));
m.insert(pair<int, int>(3, 30));
printMap(m);//会按照key值从小到大自动排序
//拷贝构造
map<int, int>m2(m);
//赋值
map<int, int>m3;
m3 = m2;
}
map中所有元素都是成对出现的,插入时要使用对组。对组的语法:pair<数据类型1, 数据类型2>(赋值1, 赋值2);
8.3 map大小和交换
功能描述:
- 统计map容器的大小以及交换map容器
函数原型:
empty();
//判断容器是否为空size();
//返回容器中元素的个数swap(st);
//交换两个集合容器
8.4 map插入和删除
功能描述:
- 对map容器进行插入、删除操作
函数原型:
insert(elem);
//在容器中插入元素clear();
//清除容器中所有元素erase(pos);
//删除pos迭代器所指的数据,返回下一个元素的迭代器erase(beg, end);
//删除[beg, end)区间的元素,返回下一个元素的迭代器erase(key);
//删除容器中值为key的元素
//创建map容器
map<int, int>m;
//插入的不同方式(建议用前两种)
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);//删掉key为3的值
若容器的size为4,但使用命令cout << m[5] << endl;
那么编译器会自动先生成一个键为5,值为0的对组,所以不太建议使用这种方式去插入,但可以利用key访问到value。
8.5 map查找和统计
功能描述:
- 对map容器进行查找数据以及统计数据
函数原型:
find(key);
//在查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end()count(key);
//统计key的元素个数
map不允许插入重复的key,哪怕value不同;multimap允许插入
8.6 map容器排序
功能描述:
- map容器默认排序规则为按照key值从小到大排序,但排序规则可以改变
主要技术点:
- 利用仿函数,可以改变排序规则
class MyCompare
{
public:
bool operator()(int v1, int v2)const
{
//降序
return v1 > v2;
}
};
void test01()
{
map<int, int, MyCompare>m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
m.insert(make_pair(5, 50));
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << (*it).first << " value = " << it->second << endl;
}
}
8.7 案例-员工分组
案例描述:
- 今天公司招聘了10名员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在哪个部门工作
- 员工信息有:姓名、工资组成;部分分为:策划、美术、研发
- 随机给10名员工分配部门和工资
- 通过multimap进行信息的插入 key(部门编号),value(员工)
- 分部门显示员工信息
#include<iostream>
#include<string>
using namespace std;
#include<map>
#include<vector>
#include<ctime>
#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;//10000,19999
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 depId = rand()%3;//0 1 2
//将员工插入到分组中
m.insert(make_pair(depId, *it));
}
}
void showWorkerByGroup(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);
//2、员工分组
multimap<int, Worker>mWorker;
setGroup(vWorker, mWorker);
//3、分组显示员工
showWorkerByGroup(mWorker);
system("pause");
return 0;
}
【未完待续】