目录
类似于Python中的字典
一、pair(对组)
- pair<type, type> p (value1, value2);
- pair<type, type> p = make_pair(value1, value2);
#include<iostream>
using namespace std;
#include<set>
// pair 队组的创建
int main()
{
cout << "对组的创建方式" << endl;
// 成对出现的数据
// 泛型编程
cout << "第一种方式用有参构造的方式创建对组,通过\"first\"和\"second\"来访问\"pair\"中的键和值" << endl;
pair<string, int> p("TOM", 20);
cout << "姓名:" << p.first << "\t年龄:" << p.second << endl;
// 第二种创建方式
cout << "用\"mack_pair()\"的方式来创建对组" << endl;
pair<string, int> p2 = make_pair("Jerry", 23);
cout << "姓名:" << p2.first << "\t年龄:" << p2.second << endl;
return 0;
}
样例输出
二、map基本概念
1. 功能
- map 中所有元素都是pair
- pair 中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
- 所有元素都会根据元素的键值自动排序
2. 本质:map/multimap 属于关联式容器,底层是用二叉树实现的
3. 优点:可以根据key值快速找到value值
4. map和multimap的区别:
map 不允许容器中有重复的key值元素
multimap 允许容器中有重复的key值元素
三、map构造和赋值
map容器中所有的元素都是成对出现,插入数据的时候要用对组
1.构造:
- map <T1, T2> mp; // map默认构造函数
- map(const map & mp); // 拷贝构造函数
2.赋值:
- map & opertor=(const map & mp); // 重载等号操作符
#include<iostream>
using namespace std;
#include<map>
// 输出
void print_map(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;
}
int main()
{
// 创建map容器
map<int, int> m;// 无论插入的顺序是什么,输出都会是升序按照key的大小
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(3, 20));
m.insert(pair<int, int>(2, 30));
m.insert(pair<int, int>(4, 40));
cout << "无论插入的顺序是什么,输出都会是升序按照key的大小" << endl;
cout << "用\"insert()\"的方式创建一个map容器,为m" << endl;
print_map(m);
cout << "用拷贝构造的方式给m2进行赋值" << endl;
// 拷贝构造
map<int, int> m2(m);
print_map(m2);
cout << "用\"=\"的方式,给m3进行赋值" << endl;
// 赋值
map<int, int>m3;
m3 = m2;
print_map(m3);
return 0;
}
输出样例
四、map大小和交换
- size(); // 返回容器中元素个数
- empty(); // 判断容器是否为空
- swap(); // 交换两个集合容器
#include<iostream>
using namespace std;
#include<map>
// 输出
void print_map(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;
}
// map 容器的大小和交换
int main()
{
// 创建map容器
map<int, int> m;// 无论插入的顺序是什么,输出都会是升序按照key的大小
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
cout << "用\"insert()\"方法给map容器中插入一些初始的数据" << endl;
print_map(m);
// 判空
cout << "用\"empty()\"方法,判断一个容器是否为空" << endl;
if (m.empty())
cout << "m 为空" << endl;
else
cout << "m 不为空" << endl;
cout << "输出map容器的大小" << endl;
// 大小
cout << "m 的大小:" << m.size() << endl;
cout << "交换两个map容器" << endl;
// 交换
map<int, int> m1;
m1.insert(pair<int, int>(5, 100));
m1.insert(pair<int, int>(6, 200));
m1.insert(pair<int, int>(7, 300));
m1.insert(pair<int, int>(8, 400));
cout << "交换前:" << endl;
cout << "m1" << endl;
print_map(m);
cout << "m2" << endl;
print_map(m1);
cout << "交换后:" << endl;
m.swap(m1);
cout << "m1" << endl;
print_map(m);
cout << "m2:" << endl;
print_map(m1);
return 0;
}
样例输出
五、map 插入和删除
- insert(elem); // 在容器中插入元素
- erase(pos); // 删除pos迭代器所指的元素,并返回下一个元素的迭代器
- erase(beg, end); // 删除(beg,end)区间内的所有元素,返回下一个元素的迭代器
- erase(key); // 删除容器给i中值为key的元素
- clear(); // 删除容器中所有元素
#include<iostream>
using namespace std;
#include<map>
// 输出
void print_map(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;
}
// map 容器的插入和删除
int main()
{
// 创建map容器
map<int, int> m;// 无论插入的顺序是什么,输出都会是升序按照key的大小
// 插入 insert
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
cout << "直接给map容器里中插入一些对组类型的初始的数据" << endl;
print_map(m);
cout << "用\"make_pair()\"的方式给map容器插入一些新的数据" << endl;
// make_pair
m.insert(make_pair(5, 50));
print_map(m);
cout << "用\"value_type()\"的方式插入新的数据" << endl;
// 第三种
m.insert(map<int, int>::value_type(6, 60));
print_map(m);
cout << "通过\"[]\"方式索引" << endl;
// 第四种
m[7] = 70;
print_map(m);
cout << "判断map容器中第4个位置是什么值" << endl;
cout << m[4] << endl; // [] 用于锁定value的位置
cout << "key不存在默认补充零 " << m[8] << endl; // 如果map中没有给键添加数据,那么会自动补零
// 删除
cout << "通过\"erase()\"传入迭代器参数,删除第一个元素" << endl;
m.erase(m.begin()); // 传入的参数是迭代器
print_map(m);
// 可以直接传入 key 值进行删除
cout << "按照key值删除某一个元素,删除\"key=3\"的键值对" << endl;
m.erase(3);
print_map(m);
// 区间删除
cout << "通过传入迭代器区间,进行区间删除" << endl;
m.erase(m.begin(), m.end()); // 全部删除
print_map(m);
if (m.empty())
{
cout << "map容器为空" << endl;
}
//删除函数
m.clear();
cout << "通过\"clear()\"方法清空map容器" << endl;
print_map(m);
if (m.empty())
{
cout << "map容器为空" << endl;
}
return 0;
}
样例输出
六、map 查找和统计
- find(key); // 查找key是否存在,返回该键的元素迭代器,若不存在,返回map.end();
- count(key); // 统计key的元素个数
#include<iostream>
using namespace std;
#include<map>
// 输出
void print_map(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;
}
// map 容器的查找和统计
int main()
{
// 创建map容器
map<int, int> m;// 无论插入的顺序是什么,输出都会是升序按照key的大小
// 插入 insert
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(2, 20));
m.insert(pair<int, int>(3, 30));
m.insert(pair<int, int>(4, 40));
print_map(m);
// 查找
map<int, int>::iterator pos = m.find(1); // 返回的是迭代器
if (pos != m.end())
cout << "找到了位于 " << pos->first << " 值为 " << pos->second << " 的元素" << endl;
else
cout << "该元素不存在" << endl;
// 统计
int num = 0;
cout << "统计map容器中8的个数" << endl;
num = m.count(8); // map 容器中一个 key 只存在一个,不能重复添加一个 key值对应多个value值
cout << "num = " << num << endl;
return 0;
}
样例输出
七、map 排序
- map容器按照key值进行排序,从小到大
- 利用仿函数,可以改变排序规则
#include<iostream>
using namespace std;
#include<map>
class my_compare
{
public:
bool operator()(int v1,int v2) // 重载()
{
// 仿函数
// 指定排序方式为降序
return v1> v2;
}
};
// 输出
void print_map(map<int,int,my_compare> &m)
{
for(map<int,int,my_compare>::iterator it=m.begin();it!=m.end();it++)
{
cout<<"key = "<<it->first<<" value = "<<it->second<<endl;
}
cout<<endl;
}
// map 容器的排序
int main()
{
// 创建map容器
map<int,int,my_compare> m;// 无论插入的顺序是什么,输出都会是升序按照key的大小
// 插入 insert
m.insert(pair<int,int>(1,10));
m.insert(pair<int,int>(2,20));
m.insert(pair<int,int>(3,30));
m.insert(pair<int,int>(4,40));
m.insert(pair<int,int>(5,50));
print_map(m);
return 0;
}
样例输出