一、简介
1、map中所有元素都是pair
2、pair中第一个元素为键值(key),第二个元素为value(实值)。键值起到索引作用
3、所有的元素都会根据元素的键值(key)自动排序
二、本质
关联性容器,底层结构使用二叉树实现
三、优点
可以根据key值快速找到value
四、map与multimap的区别
map不允许容器中有重复的key值元素
multimap中允许有重复的key值元素
五、map的构造和赋值
1、构造
默认构造函数:map<T1, T1> mp;
考妣构造函数:map(const map &mp);
2、赋值
map& operator = (const mapp &mp); //重载等号操作符
#include <iostream>
#include<map>
//打印输出接口
void PrintMap( std::map<int, int>&m) {
for (std::map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
std::cout << "key值 = " << (*it).first <<", "<< "value = " << it->second << std::endl;
}
std::cout << std::endl;
}
//默认构造函数创建
void test01() {
//用默认构造函数创建map容器
std::map<int, int> m;
//给m容器中插入数据
m.insert(std::pair<int, int>(1, 10));
m.insert(std::pair<int, int>(2, 20));
m.insert(std::pair<int, int>(3, 30));
m.insert(std::pair<int, int>(4, 40));
/*1,2,3,4表示key值,10,20,30,40表示value值*/
std::cout << "test01中m容器中的的值" << std::endl;
PrintMap(m);
}
void test02() {
//用默认构造函数创建map容器
std::map<int, int> m;
//给m容器中插入数据
m.insert(std::pair<int, int>(5, 50));
m.insert(std::pair<int, int>(7, 70));
m.insert(std::pair<int, int>(8, 80));
m.insert(std::pair<int, int>(6, 60));
/*虽然key值的输入顺序为5,7,8,6,但是输出时会按照key值的5,,6,7,8的顺序排序输出*/
std::cout << "test02中m容器中的的值" << std::endl;
PrintMap(m);
//用拷贝构造函数创建容器
std::map<int, int>m2(m);
std::cout << "m2容器中的的值" << std::endl;
PrintMap(m2);
//给容器元素赋值
std::map<int, int>m3;
m3 = m2;
std::cout << "m3容器中的的值" << std::endl;
PrintMap(m3);
}
int main()
{
test01();
test02();
system("pause");
return 0;
//std::cout << "Hello World!\n";
}
swap()交换两个map容器
#include <iostream>
#include<Map>
//打印map
void PrintMap(std::map<int, int>&m) { //&是以引用的方式传入
//输出容器中的值
for (std::map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
std::cout << "key = " << (*it).first << "value = " << it->second << std::endl;
}
std::cout << std::endl;
}
void test01() {
//创建一个map容器
std::map<int, int>m;
//想容器中插入数据
m.insert(std::pair<int, int>(1, 10));
m.insert(std::pair<int, int>(2, 20));
//判断容器是否为空
if (m.empty()) {
std::cout << "容器为空" << std::endl;
}
else {
std::cout << "容器不为空" << std::endl;
std::cout << "m的大小" << m.size() << std::endl;
}
}
//map的swap()交换
void test02() {
std::map<int, int> m1;
m1.insert(std::pair<int, int>(1, 10));
m1.insert(std::pair<int, int>(2, 20));
std::map<int, int> m2;
m2.insert(std::pair<int, int>(1, 100));
m2.insert(std::pair<int, int>(2, 200));
std::cout << "交换前:" << std::endl;
PrintMap(m1);
PrintMap(m2);
m1.swap(m2); //交换两个容器
PrintMap(m1);
PrintMap(m2);
}
int main()
{
test01();
test02();
system("pause");
//std::cout << "Hello World!\n";
}
map的插入和删除
//map的插入和删除
#include <iostream>
#include<map>
void PrintMap(std::map<int, int>&m) {
for (std::map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
std::cout << "key = " << it->first << '\t' << "value = " << it->second << std::endl;
}
std::cout << std::endl;
}
void test01() {
std::map<int, int>m;
//插入
//第一种
m.insert(std::pair<int, int>(1, 10));
//第二种
m.insert(std::make_pair(2, 20));
/*std::make_pair(2, 20):插入key=2,value=20的数据*/
//第三种
m.insert(std::map<int, int>::value_type(3, 30));
//第四种,
m[4] = 40;
PrintMap(m);
//删除
m.erase(m.begin()); //删除第一个元素
PrintMap(m);
m.erase(3); //按照key删除,不会按照value删除
PrintMap(m);
m.clear(); //清空容器
//m.erase(m.begin(), m.end());
PrintMap(m);
}
int main()
{
test01();
system("pause");
//std::cout << "Hello World!\n";
}
/*
函数原型:
inser(elem); //在容器中插入
clear(); //清楚所有元素,返回下一个元素的迭代器
erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end); //删除区间[begin,end)的所有元素,返回下一个元素的迭代器
erase(key); //删除容器中值为key的值
*/
map的查找、统计、排序操作
#include <iostream>
#include<map>
//改变排序规则的仿函数
class MyCompare {
public:
//如果是做两个数的对比操作,返回值是bool类型
bool operator()(int v1, int v2) { //第一个()表示重载运算符,第二个()表示参数列表
//降序
return v1 > v2;
}
};
void test01() {
//查找
std::map<int, int>m;
m.insert(std::make_pair(1, 10));
m.insert(std::pair<int, int>(2, 20));
m.insert(std::pair<int, int>(3, 30));
//查找有没有key = 3的数据
std::map<int, int>::iterator it = m.find(3);
/*无论查找成功还是失败,都会返回一个迭代器*/
//判断查找陈宫还是失败
if (it != m.end()) {
std::cout << "找到了该元素" << std::endl;
std::cout << "key = " << (*it).first << '\t' << "value = " << it->second << std::endl;
}
else {
std::cout << "没有找到该元素" << std::endl;
}
//统计
int num = m.count(3); //统计key = 3的值,因为map中key值不能重复出现,所以count(3)的值只会为 0 或 1 ,
std::cout << "key = 3的数量有:" << num << std::endl;
}
//排序,利用仿函数
void test02() {
std::map<int, int,MyCompare>m;
m.insert(std::make_pair(1, 10));
m.insert(std::pair<int, int>(2, 20));
m.insert(std::pair<int, int>(3, 30));
m.insert(std::pair<int, int>(4, 40));
m.insert(std::pair<int, int>(5, 50));
std::cout << "降序排列结果:" << std::endl;
for (std::map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
std::cout << "key = " << (*it).first << ",\t value = " << it->second << std::endl;
}
}
int main()
{
test01();
test02();
system("pause");
//std::cout << "Hello World!\n";
}