一、map容器的基本概念
简介:
map中所有元素都是pair
pair中的第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
所有元素都会根据元素的键值自动排序
本质:map/multimap属于关联式容器,底层结构是二叉树实现。
优点:可以根据key值快速找到value值
map与multimap的区别:
map不允许容器中有重复的key值元素。
multimap允许容器中有重复的key值元素。
二、map构造和赋值
学习目标:对map容器进行构造和赋值操作
构造函数原型:
map<T1,T2> mp; //默认构造
map(const map &mp); //拷贝构造
赋值函数原型:
map& operator=(const map &mp); //重载等号操作符
示例:
#include "iostream"
#include <map>
#include <string>
using namespace std;
void PrintMap01(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 MapTest39()
{
//两个数据类型,一个代表key值,一个代表value值
map<int, int> m;
//m.insert(2, 10); //非法的间接寻址
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(4, 40));
m.insert(pair<int, int>(3, 30));
PrintMap01(m);
//2、拷贝构造
cout << "拷贝构造:" << endl;
map<int, int> m2(m);
PrintMap01(m2);
//3、重载等号操作符
cout << "重载等号操作符:" << endl;
map<int, int> m3;
m3 = m;
PrintMap01(m3);
}
int main()
{
MapTest39();
system("pause");
return 0;
}
总结:map中的所有元素都是成对出现的,插入数据时要使用对组
三、map容器的大小与交换
学习目标:统计map容器的大小与交换map容器
函数原型:
size(); //返回容器中元素个数
empty();
swap(st); //交换两个集合容器
示例:
#include "iostream"
#include <map>
#include <string>
using namespace std;
void PrintMap02(const map<int, string> &m)
{
for (map<int, string>::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << "Key=" << it->first << " Value=" << (*it).second << endl;
}
cout << endl;
}
void MapTest40()
{
map<int, string> m;
//1、插入
m.insert(pair<int, string>(1, "老一"));
m.insert(pair<int, string>(2, "老二"));
m.insert(pair<int, string>(3, "老三"));
m.insert(pair<int, string>(4 ,"老四"));
m.insert(pair<int, string>(5, "老五"));
PrintMap02(m);
//2、大小
if (!m.empty())
{
cout << "m容器的大小为:" << m.size() << endl;
}
else
{
cout << "m容器为空!" << endl;
}
//3、交换
map<int, string> m1;
//1、插入
m1.insert(pair<int, string>(1, "科一"));
m1.insert(pair<int, string>(2, "科二"));
m1.insert(pair<int, string>(3, "科三"));
cout << "交换前:" << endl;
PrintMap02(m);
PrintMap02(m1);
cout << "交换后:" << endl;
m1.swap(m);
PrintMap02(m);
PrintMap02(m1);
}
int main()
{
MapTest40();
system("pause");
return 0;
}
四、map容器的插入与删除
学习目标:对map容器进行数据的插入与删除
函数原型:
insert(elem); //在容器中插入元素
clear(); //清除所有元素
erase(pos); //删除pos迭代器所指向的元素,返回下一个元素的迭代器
erase(beg,end) //删除[beg,end)区间的迭代器所指向的元素,返回下一个元素的迭代器
erase(key); //删除容器中值为key的元素
示例:
#include "iostream"
#include <map>
#include <string>
using namespace std;
void PrintMap03(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 MapTest41()
{
map<int, int> m;
//1、插入
//第一种插入
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; //不建议使用,但可以用key来访问到value
cout << m[5] << endl;
PrintMap03(m);
//2、删除erase()
m.erase(m.begin());
PrintMap03(m);
m.erase(3);
PrintMap03(m);
m.erase(m.begin(), m.end());
PrintMap03(m);
m.insert(make_pair(5, 50));
PrintMap03(m);
cout << "清除操作:" << endl;
m.clear();
PrintMap03(m);
}
int main()
{
MapTest41();
system("pause");
return 0;
}
五、map容器的查找与统计
学习目标:对map容器进行数据查找与统计
函数原型:
find(key); //查找key元素是否存在,若存在,则返回迭代器,若不存在,返回set.end();
count(key); //统计key的元素个数
示例:
#include "iostream"
#include <map>
#include <string>
using namespace std;
void MapTest42()
{
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;
//查找
map<int, int>::iterator pos = m.find(3);
if (pos != m.end())
{
cout << "查找到了元素: Key=" << pos->first << " Value=" << (*pos).second << endl;
}
else
{
cout << "没有查找到元素" << endl;
}
//统计 map容器中不允许插入重复的key值 对于count统计要么是0,要么是1
int num = m.count(3);
cout << "Num=" << num << endl;
}
int main()
{
MapTest42();
system("pause");
return 0;
}
六、map容器的排序
学习目标:利用仿函数,改变排序规则
map容器的默认排序规则:按照key值进行,从小到大排序
示例:
#include "iostream"
#include <map>
#include <string>
using namespace std;
class MapPerson {
public:
MapPerson() {}
MapPerson(string name, int age)
{
this->M_name = name;
this->M_age = age;
}
string M_name;
int M_age;
};
class MapCompare {
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
bool operator()(MapPerson p1, MapPerson p2) //不能传入地址或引用
{
return p1.M_age < p2.M_age;
}
};
class MapComparePER {
public:
bool operator()(MapPerson p1, MapPerson p2)
{
return p1.M_age > p2.M_age;
}
};
void MapTest43()
{
map<int, int, MapCompare> m; //加仿函数后,后面的排序规则会根据仿函数的规则进行排序
m.insert(make_pair(5, 50));
//m[5] = 50; //这种ing不属于(int,int)表达式,比较时会报错错
m.insert(pair<int, int>(1, 10));
m.insert(map<int, int>::value_type(3, 30));
m.insert(make_pair(2, 10));
m.insert(pair<int, int>(4, 40));
cout << "map容器的默认排序:" << endl;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "Key=" << it->first << " Value=" << (*it).second << endl;
}
//改变map容器的默认排序规则
cout << "改变map容器的默认排序规则" << endl;
for (map<int, int, MapCompare>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "Key=" << it->first << " Value=" << (*it).second << endl;
}
map<MapPerson,int, MapCompare > mp;
MapPerson p1("小黄", 22);
MapPerson p3("小红", 2);
MapPerson p2("小新", 12);
MapPerson p4("小东", 32);
mp.insert(pair<MapPerson, int>(p1, 1));
mp.insert(pair<MapPerson, int>(p4, 4));
mp.insert(pair<MapPerson, int>(p3, 3));
mp.insert(pair<MapPerson, int>(p2, 2));
cout << "自定义数据类型没有默认排序规则,必须指定排序规则,否则报错。" << endl;
for (map<MapPerson, int>::iterator it = mp.begin(); it != mp.end(); it++)
{
cout << "Key=" << (*it).first.M_name << "+" << (*it).first.M_age << " Value=" <<it->second<<endl;
}
cout << "自定义数据类型自定义排序后:" << endl;
for (map<MapPerson, int, MapCompare >::iterator it = mp.begin(); it != mp.end(); it++)
{
cout << "Key=" << (*it).first.M_name << "+" << (*it).first.M_age << " Value=" << it->second << endl;
}
}
int main()
{
MapTest43();
system("pause");
return 0;
}
总结:对于内置的数据类型,map容器有自己的默认排序规则。但对于自定义数据类型,map容器必须指定排序规则,否则,会报错