#include<iostream>
#include<map>
using namespace std;
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>(3, 20));
m1.insert(pair<int, int>(2, 30));
m1.insert(pair<int, int>(4, 40));
printMap(m1);
map<int, int>m2(m1);
printMap(m2);
map<int, int>m3;
m3 = m2;
printMap(m3);
}
map也采用insert()添加值,对对组要指明两个参数的类型,first代表键值key,second则是存的value值
void test02()
{
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 << "m为空" << endl;
else
{
cout << "m不为空" << endl;
cout << "m的大小为:" << m1.size() << endl;
}
map<int, int>m2;
m2.insert(pair<int, int>(4, 37));
m2.insert(pair<int, int>(5, 73));
m2.insert(pair<int, int>(6, 373));
cout <<"交换前:" << endl;
printMap(m1);
printMap(m2);
m1.swap(m2);
cout << "交换后:" << endl;
printMap(m1);
printMap(m2);
}
size返回数据个数,使用swap交换
void test03()
{
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;
cout << m[5] << endl;
printMap(m);
m.erase(m.begin());
printMap(m);
m.erase(3);
printMap(m);
m.clear();
}
map插入数据有四种方式
建议使用第二种,比较简单方便,第四种虽然简单,但在实际应用中容易误改数据值或是被当作数组难以分清
删除数据使用erae函数,可传迭代器删除,也可传值删除,clear清除函数就是erase从头到尾。
对m[5]未赋值打印可以看出,map默认value值为0,
且打印也是按键值顺序打印的
void test04()
{
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));
m1.insert(pair<int, int>(3, 40));
if (m1.find(3) != m1.end())
cout << "找到了 key=" << m1.find(3)->first << ",value=" << m1.find(3)->second << endl;
else
cout << "没找到" << endl;
int num = m1.count(3);
cout << "num=" << num << endl;
}
使用find函数查找,参数是value值,
计数函数count再map里只能返回0或一,因为键值不能重复,所以只能找到一个对应的或找不到
class cmp
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test05()
{
map<int, int,cmp>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,cmp>::iterator it=m.begin();it!=m.end();it++)
cout << "key=" << it->first << ",value=" << it->second << endl;
}
map默认升序排序,想要实现其他类型排序,需要自定义比较类,类中重载括号(),即仿函数,来自定义比较方式。创建map容器对象时,需要传入类名。
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<time.h>
using namespace std;
#define 策划 0 //宏定义部门编号
#define 美术 1
#define 研发 2
class worker
{
public:
string m_name;
int m_salary;
};//员工类
void creatWorker(vector<worker>&v)
{
string namseed = "ABCDEFGHIJ";//名称种子
for (int i = 0; i < 10; i++)
{
worker p;
p.m_name = "员工";
p.m_name += namseed[i];
p.m_salary = rand() % 10000 + 10000;//工资10000~19999
v.push_back(p);//存入vector容器中
}
}
void setgroup(vector<worker>&v, multimap<int, worker>&m)
{
for (vector<worker>::iterator i = v.begin(); i != v.end(); i++)
{
int rid = rand() % 3;//随机生成部门编号
m.insert(make_pair(rid, *i));//将编号以及vector容器中存的员工信息插入到map容器中
}
}
void showworker(multimap<int, worker>&m)
{
cout << "策划部门:" << endl;
multimap<int,worker>::iterator pos=m.find(策划);利用find查找key值,即部门
int count = m.count(策划);//统计部门人数
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(美术);
count = m.count(美术);
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(研发);
count = m.count(研发);
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));//设置时间戳生成真实随机数
vector<worker>vp;
creatWorker(vp);
multimap<int, worker>mp;
setgroup(vp,mp);
showworker(mp);
return 0;
}