1.map概念:map中所有元素都是pair
pair中的第一个元素为key(键值)起到索引作用,第二个为value(实值)
所有元素都会根据key值自动排序
本质:map/multimap属于关联式容器,底层结构是用二叉树实现
优点:可以根据key值快速找到value值
map和/multimap区别:是否允许有相同的key值(都允许有重复的value值)
2.map的构造和赋值
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<int, int> m1;m1.insert(pair<int, int>(1, 10));//匿名队组实现插入m1.insert(pair<int, int>(2, 20));//插入按照key值自动排序m1.insert(pair<int, int>(4, 40));m1.insert(pair<int, int>(3, 30));m1.insert(pair<int, int>(5, 50));PrintMap(m1);map<int, int> m2(m1);//拷贝构造PrintMap(m2);map<int, int> m3;m3 = m1;PrintMap(m3);//operator=赋值
}
3.map容器的大小和交换
4.map容器中插入和删除
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<int, int> m1;m1.insert(pair<int, int>(1, 10));//匿名队组实现插入m1.insert(make_pair(2, 20));//用make_pair实现m1.insert(map<int, int>::value_type(3, 30));//用value_type实现m1[4] = 40;//用[]实现;cout << m1[5] << endl;//不建议用[]访问,如果不存在会自动插入<5,0>PrintMap(m1);m1.erase(m1.begin());//迭代器删除PrintMap(m1);m1.erase(2);//按照key值删除PrintMap(m1);//清空//m1.erase(m1.begin(), m1.end());//按照区间删除//PrintMap(m1);m1.clear();PrintMap(m1);
}
5.map容器的查找和统计
6.map容器的排序操作
默认按照key值从小到大排序,用仿函数可以自定义排序规则
class MyCompare
{
public:bool operator()(int v1,int v2) const//仿函数{return v1 > v2;//比较的是两个队组的key值}
};
void test01()
{map<int, int,MyCompare> m1;m1.insert(pair<int, int>(1, 10));//匿名队组实现插入m1.insert(pair<int, int>(2, 20));//插入按照key值自动排序m1.insert(pair<int, int>(4, 40));m1.insert(pair<int, int>(3, 30));m1.insert(pair<int, int>(5, 50));for (map<int, int, MyCompare>::iterator it = m1.begin(); it != m1.end(); it++){cout << "key值=" << it->first << " value=" << it->second << endl;}
}
7.STL案例,员工分组
class Worker
{
public:string m_name;int m_salary;
};
//创建员工
void CreatPerson(vector<Worker>& v)
{string PersonSeed = "ABCDEFGHIJ";for (int i = 0; i < 10; i++){Worker worker;//注意要先创建员工再插入vector容器string name = "员工";name += PersonSeed[i];worker.m_name = name;worker.m_salary = rand() % 10000 + 10000;//10000--19999v.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 2m.insert(make_pair(depid, *it));}
}
//打印员工信息
void PrintWorker(multimap<int, Worker>& m)
{multimap<int,Worker>::iterator pos=m.find(0);int num = m.count(0);cout << "策划部门:" << endl;for (int i = 0; i < num&&pos!=m.end(); pos++,i++){cout << "员工部门:" << pos->first << " 姓名:" << (pos->second).m_name << " 员工薪资:" << (pos->second).m_salary << endl;}cout << "------------------------------------------------" << endl;pos = m.find(1);num = m.count(1);cout << "美术部门:" << endl;for (int i = 0; i < num && pos != m.end(); pos++,i++){cout << "员工部门:" << pos->first << " 姓名:" << (pos->second).m_name << " 员工薪资:" << (pos->second).m_salary << endl;}cout << "------------------------------------------------" << endl;pos = m.find(2);num = m.count(2);cout << "研发部门:" << endl;for (int i = 0; i < num && pos != m.end(); pos++,i++){cout << "员工部门:" << pos->first << " 姓名:" << (pos->second).m_name << " 员工薪资:" << (pos->second).m_salary << endl;}cout << "------------------------------------------------" << endl;
}
void test01()
{srand((unsigned int)time(NULL));//随机数种子vector<Worker> vWorker;//存储员工的vector容器//1.创建员工CreatPerson(vWorker);multimap<int, Worker> mWorker;//key值为员工部门编号,value值为员工//2.员工分部门SetGroup(vWorker,mWorker);//3.显示员工及其部门PrintWorker(mWorker);
}