版权声明:内容若有错误,请您务必指出,感谢让我提高并给予我建议的你! 转载请注明出处 https://blog.csdn.net/qq792358814/article/details/81363024
Map笔记
- map简介
- map是关联容器,string、vector、list、deque是顺序容器。
- 关联容器分为有序与无序。有序容器的底层数据结构是红黑树,无序的是散列表。
- Map的结构式<key|value>前面是键值,后面是实值。前面键值不允许重复,(但是multimap容器值允许重复)(set容器键值就是实值,元素只包含一个)(multiset也不允许重复)
- 红黑树——平衡 排序二叉树——x=log2^N(N是数据总量,x是查找次数,2^x>=N)
- Map优点:1000个数据,只需要查找1~10次,而list需要查找1~1000次(2^x>=N)。如果数据量大的话,使用map是个不错的选择,数据量少map就大材小用,可以使用list。Map插入效率低于链表,因为涉及到排序。
- map构造函数
- map插入元素需要结构体,vs给我们定义了结构体函数pair<int ,char>(1,’a’);用法:mp.insert(pair<int, char>(1,'a'));
- 也可以冲定义结构体函数typedef pair<int, char> in_pair,可以直接mp.insert(in_pair(2, 'b'));
要确认是否键值重复,可以用返回值来确定。pair<map<int, char>::iterator, bool> pr; //返回值 ,定义 pr = mp.insert(pair<int, char>(1, 'a'));,输出cout << pr.second << endl;
- Map定义
- Size计算map的大小,count(1)判断是否存在这个“1”的键值,有就1没就0;empty()判断容器是否为空。
- 迭代器的输出看代码的60行
- Map增删改查
- Map的键值不能改,实值可以改
- 可以通过迭代器来查找键值为2的元素,map<int, char>::iterator ite1 = mp.find(2);//通过键值查找,如果找不到,则程序崩溃
- Map自动排序改至从大到小排序,则在map定义的时候加上greater函数,要记得functional头文件,在迭代器的那map也要同时加上greater,写法: map<int, char,greater<int>> mp;
- Map尾元素
- Mp.end()是插入的最后一个函数的下一个元素,而不是最后一个。
- Lower_bound函数,返回参数key位置,如果没有这个参数,则返回>这个参数的下一个参数
- Upper_bound函数,比如说,返回比参数大的下一个相邻参数,参数是1258,比方说2就返回5,3也返回5。
#include <iostream>
#include <map>
#include <algorithm>
#include <functional>
using namespace std;
void fun(pair<int,char> pr)
{
cout << pr.first << " " << pr.second << endl;
}
void map_construct()
{
typedef pair<int, char> in_pair;//重命名函数
pair<map<int, char>::iterator, bool> pr; //返回值
map<int,char> mp;//键值盾<>有两个元素
map<int, string> mp1;//<>里面可以放很多种类
pr = mp.insert(pair<int, char>(1, 'a'));
//cout << pr.second << endl;//查看是否能够插入,成功出1,失败出0
pr = mp.insert(in_pair(2, 'b'));
//cout << pr.second << endl;
pr = mp.insert(in_pair(2, 'c'));//键值不能相同
//cout << pr.second << endl;//键值相同,插入失败,出0
pr = mp.insert(in_pair(7, 'b'));
pr = mp.insert(in_pair(9, 'b'));
pr = mp.insert(in_pair(11, 'b'));
pr = mp.insert(in_pair(13, 'b'));
map<int, char>::iterator ite = mp.begin();
ite++;ite++; ite++; ite++;
mp.insert(ite,in_pair(3, 'b'));//证明会自动排序
//cout << &ite << endl;
map<int,char> mp2;
mp2.insert(mp.begin(), mp.end());
map<int, char> mp3(mp);
map<int, char> mp4(mp.begin(), mp.end());
for_each(mp3.begin(),mp3.end(),fun);
}
void map_define()
{
typedef pair<int, char> in_pair;
map<int, char> mp;//键值盾<>有两个元素
mp.insert(in_pair(1, 'b'));
mp.insert(in_pair(2, 'b'));
mp.insert(in_pair(3, 'b'));
mp.insert(in_pair(4, 'b'));
mp.insert(in_pair(5, 'b'));
//cout << mp.size() << endl;
//mp.insert(in_pair(6, 'b'));
//cout << mp.size() << endl;
//cout << mp.count(11) << endl;//没有这键值,返回0,有的话就返回1
//cout << mp.empty() << endl;
map<int, char>::iterator ite = mp.begin();
for (ite; ite != mp.end(); ite++)
{
cout << ite->first << " " << ite->second << endl;
}//作用于for_each一样
//for_each(mp.begin(), mp.end(), fun);
}
void map_clear()
{
typedef pair<int, char> in_pair;
map<int, char,greater<int>> mp;
mp.insert(in_pair(1, 'b'));
mp.insert(in_pair(2, 'b'));
mp.insert(in_pair(3, 'b'));
mp.insert(in_pair(4, 'b'));
mp.insert(in_pair(5, 'b'));
map<int, char,greater<int>>::iterator ite = mp.begin();
ite++;
//mp.erase(ite);//ite=1,删除第二个元素
//mp.erase(ite,mp.end());//第二个删到最后一个
mp.erase(3);//删除键值为3的
map<int, char,greater<int>>::iterator ite1 = mp.find(2);//通过键值查找
cout << ite1->first << " " << ite1->second << endl;
for_each(mp.begin(), mp.end(), fun);
}
void map_tail()
{
typedef pair<int, char> in_pair;
map<int, char> mp;
mp.insert(in_pair(1, 'b'));
mp.insert(in_pair(9, 'b'));
mp.insert(in_pair(3, 'b'));
mp.insert(in_pair(4, 'b'));
mp.insert(in_pair(5, 'b'));
cout << mp.find(5)->first << endl;//返回所在节点迭代器
if (mp.end() == mp.find(6))//end是5的下一个元素
{
cout << "OK" << endl;
}
cout << mp.lower_bound(4)->first << endl;//返回参数key位置,如果没有这个参数,则返回>这个参数的下一个参数
cout << mp.upper_bound(4)->first << endl;//返回比参数大的下一个相邻参数,比方说2就返回5,3也返回5。
//for_each(mp.begin(), mp.end(), fun);
}
void _multimap()
{
typedef pair<int, char> in_pair;
multimap<int, char> mmp;
mmp.insert(in_pair(1, 'a'));
mmp.insert(in_pair(1, 'a'));
mmp.insert(in_pair(1, 'a'));
mmp.insert(in_pair(1, 'a'));
cout << mmp.count(1) << endl;//计算多少个键值为1的元素
cout << mmp.find(1)->first << endl;
multimap<int, char>::iterator ite = mmp.begin();
for (ite; ite != mmp.end(); ite++)
{
cout << ite->first << " " << ite->second << endl;
}
}
int main()
{
//map_construct();
//map_define();
//map_clear();
//map_tail();
_multimap();
system("pause");
return 0;
}