Linux C++ 032-STL之map、multimap容器
本节关键字:Linux、C++、map、multimap
相关库函数:clear、insert、erase
map基本概念
简介:
map中所有元素都是pair
pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实际值)
所有元素都会根据元素的键值自动排序
本质:map/multimap属于关联式容器,底层结构是二叉树实现
优点:
(1)可以根据key值快速找到value值
(2)map和multimap的区别:
(3)map不允许容器中有重复key值元素
(4)multimap允许容器中有重复key值元素
map构造和赋值
功能描述:对map容器进行构造和赋值操作
函数原型:
// 构造:
map<T1, T2> mp; //map默认构造函数
map(const map & mp); //拷贝构造函数
// 赋值:
map & operator=(const map & mp); //重载等号操作符
示例:
#include <map>
void printMap(const map<int, int> &m)
{
for(map<int, int>::const_iterator it = m.begin();it != m.end();it++)
{
cout << "key = " << it->first << "value = " << it->second << endl;
}
cout << endl;
}
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));
printMap(m);
map<int, int> m2(m);
printMap(m2);
map<int, int> m3;
m3 = m2;
printMap(m3);
}
map大小和交换
功能描述:统计map容器的大小以及交换map容器
函数原型:
size(); //返回容器中元素的数目
empty(); //判断容器是否为空,为空返回真
swap(st); //交换两个集合容器
示例:
#include <map>
void printMap(const map<int, int> &m)
{
for(map<int, int>::const_iterator it = m.begin();it != m.end();it++)
{
cout << "key = " << it->first << "value = " << 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));
if( m1.empty() )
{
cout << "m1为空" << endl;
}
else
{
cout << "m1不为空" << " 大小为:" << m1.size() << endl;
cout << "元素为:" << endl;
printMap(m1);
}
map<int, int> m2;
cout << "交换前" << endl;
printMap(m1);
printMap(m2);
m1.swap(m2);
cout << "交换后" << endl;
printMap(m1);
printMap(m2);
}
map插入和删除
功能描述:map容器进行插入数据和删除数据
函数原型:
insert(elem); //在容器中插入元素
clear(); //清除所有元素
erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg, end); //算出区间 [beg, end) 中的所有元素,返回下一个元素的迭代器
erase(key); //删除容器中值为key的元素
示例:
#include <map>
void printMap(const map<int, int> & m)
{
for(map<int, int>::const_iterator it = m.begin();it != m.end();it++)
{
cout << "key = " << it->first << "value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
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;//不建议用,key不存在时,使用[]访问会创value为0的对组
// [] 不建议做插入,但是可以利用key访问到value
cout << "m[4] = " << m[4] << endl;
printMap(m1);
m1.erase(m1.begin());
printMap(m1);
m.erase(3);
printMap(m1);
m1.clear();
m1.erase(m1.begin(), m1.end());
printMap(m1);
}
map查找和统计
功能描述:对map容器进行查找数据以及统计数据
函数原因:
find(key); //查找key,存在时返回该键的迭代器,若不存在,返回set.end()
count(key); //统计key的元素个数
示例:
#include <map>
void test01()
{
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));
map<int, int>::iterator pos = m.find(3);
if(pos != m.end())
{
cout << "存在,key = " << (*pos).first << "value = " << pos->second << endl;
}
else
{
cout << "不存在" << endl;
}
int num = m.count(3);
//结果要么为1,要么为0 - map不允许插入重复的key值
cout << "num = " << num << endl;
}
map容器排序
map容器默认排序规则为 按照key值进行 从小到大排序,掌握如何改变排序的规则
主要技术:利用仿函数,可以改变排序规则
示例:
#include <map>
class MyCompare
{
public:
bool operator()(int v1, int v2)
{
//从大到小排序
return v1 > v2;
}
};
void printMap(const map<int, int> & m)
{
for(map<int, int>::const_iterator it = m.begin();it != m.end();it++)
{
cout << "key = " << it->first << "value = " << it->second << endl;
}
cout << endl;
}
void test01()
{
//默认排序
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);
//自定义排序
map<int, int, MyCompare> m1;
m1.insert(pair<int, int>(5, 50));
m1.insert(pair<int, int>(1, 10));
m1.insert(pair<int, int>(2, 20));
m1.insert(pair<int, int>(4, 40));
m1.insert(pair<int, int>(3, 30));
printMap(m1);
}
总结:
利用仿函数可以指定map容器的排序规则
对于自定义数据类型,map必须要指定排序规则,同set容器
案例:员工分组
案例描述:
公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在哪个部门工作
员工信息:姓名、工资
部门分为:策划、美术、研发
随机给10名员工分配部门和工资
通过multimap进行信息的插入 key(部门编号)、value(员工)
分部门显示员工信息
案例代码:
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <ctime>
#define CEHUA 0
#define MEISHU 1
#define KAIFA 2
class Worker
{
public:
Worker(string name, int salary)
{
this->m_Name = name;
this->m_Salary = salary;
}
string m_Name;
int m_Salary;
};
void printWorker(const vector<int, Worker> & m)
{
for(vector<Worker>::const_iterator it = m.begin();it != m.end();it++)
{
cout << "员工姓名:" << it->m_Name << "员工薪资:" << it->m_Salary << endl;
}
cout << endl;
}
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 deptId = rand() % 3; // 0 1 2
//将员工插入到分组中
m.insert(make_pair(deptId, *it));
}
}
void showWorkerByGoup(const multimap<int, Worker> &m)
{
for(multimap<int, Worker>::const_iterator it = m.begin();it != m.end();it++)
{
cout << "部门编号:" << it->first << "员工姓名:" << it->second.m_Name << "员工薪资:" << it->second.m_Salary << endl;
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->t->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->t->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->t->second.m_Name << "员工薪资:" << pos->second.m_Salary << endl;
}
}
}
void main()
{
srand((unsigned int)time(NULL));
//创建员工
vector<Worker> vWorker;
createWorker(vWorker);
printWorker(vWorker);
//员工分组
multimap<int, Worker> mWorker;
setGroup(vWorker, mWorker);
//分组显示员工
showWorkerByGoup(mWorker);
system("pause");
return 0;
}