首页 > 其他分享 > STL-map/multimap容器

STL-map/multimap容器

时间:2023-02-09 16:36:12浏览次数:33  
标签:map multimap insert STL mp key pair PrintMap

简介:

  • map中所有元素都是pair
  • pair第一个元素为key键值,起到索引作用,第二个元素为value实质
  • 所有元素都会按照key键值自动排序

本质:

  • map/multimap属于关联式容器,底层结构使用二叉树实现

优点:

  • 可以根据key值快速找到value值

map和multimap区别:

  • map不允许容器中有重复的key值元素
  • multimap允许容器中有重复的key值元素

map构造和赋值

构造:

  • ​map<T1, T2>mp;​​//默认构造
  • ​map(const map& mp);​​//拷贝构造

赋值:

  • ​map& operator=(const map& mp);​​//重载=运算符

map容器所有元素都成对出现,插入数据需要使用对组

#include<iostream>
#include<string>
#include<map>
using namespace std;
void PrintMap(const map<int, int>& mp)
{
for (map<int, int>::const_iterator it = mp.begin(); it != mp.end(); it++)
{
//可以用解引用的方法,也可以用间接访问操作符->
cout << (*it).first << " " << it->second << endl;
}
cout << endl;
}
int main()
{
map<int, int>mp;
mp.insert(pair<int, int>(1, 2));
mp.insert(pair<int, int>(5, 4));
mp.insert(pair<int, int>(3, 1));
mp.insert(pair<int, int>(2, 4));
mp.insert(pair<int, int>(4, 6));
PrintMap(mp);
map<int, int>mp1;
mp1 = mp;
PrintMap(mp1);
return 0;
}


map容器大小和交换

  • ​.size();​​//返回容器中元素数目
  • ​.empty();​​//判断容器是否为空
  • ​.swap(mp);​​//交换两个集合容器
#include<iostream>
#include<string>
#include<map>
using namespace std;
void PrintMap(const map<int, int>& mp)
{
for (map<int, int>::const_iterator it = mp.begin(); it != mp.end(); it++)
{
//可以用解引用的方法,也可以用间接访问操作符->
cout << (*it).first << " " << it->second << endl;
}
cout << mp.size() << mp.empty() << endl;
}
int main()
{
map<int, int>mp;
map<int, int>mp1;
mp.insert(pair<int, int>(1, 2));
mp.insert(pair<int, int>(5, 4));
mp.insert(pair<int, int>(3, 1));
mp1.insert(pair<int, int>(2, 4));
mp1.insert(pair<int, int>(4, 6));
mp1.insert(pair<int, int>(6, 6));
PrintMap(mp);
PrintMap(mp1);
//交换
mp.swap(mp1);
//验证交换后的结果
PrintMap(mp);
PrintMap(mp1);
return 0;
}


map插入和删除

  • ​.insert(elem);​​//容器中插入元素
  • ​pair​​方式
  • ​make_pair​​方式
  • ​value_type​​方式
  • ​[]​​下标方式
  • ​.clear();​​//清除所有元素
  • ​.erase(pos);​​//删除pos迭代器所指的元素,返回下一个元素的迭代器
  • ​.erase(beg,end);​​//删除区间[beg,end)内的元素,返回下一个元素的迭代器
  • ​.erase(key);​​//删除容器中值为key的元素
#include<iostream>
#include<string>
#include<map>
using namespace std;
void PrintMap(const map<int, int>& mp)
{
for (map<int, int>::const_iterator it = mp.begin(); it != mp.end(); it++)
{
//可以用解引用的方法,也可以用间接访问操作符->
cout << (*it).first << " " << it->second << endl;
}
cout << endl;
}
int main()
{
map<int, int>mp;
//第一种插入
mp.insert(pair<int, int>(2, 1));
//第二种插入,不需要写模板参数
mp.insert(make_pair(1, 3));
//第三种,value_type值类型
mp.insert(map<int, int>::value_type(3, 6));
//第四种,不建议用于插入
mp[4] = 5;
//下标方式,如果没有找到,会创建一个实值为0的元素
//作用:可以利用key值访问到value
cout << mp[5] << endl;
PrintMap(mp);
//删除
mp.erase(mp.begin());
mp.erase(5);//按照key删除
PrintMap(mp);
return 0;
}


map查找和统计

  • ​.find(key);​​​//查找key是否存在,返回该键的元素的迭代器,若不存在,返回​​.end();​
  • ​.const(key);​​//统计key的元素个数,对于map,无非是0或1
map<int, int>mp;
mp.insert(pair<int, int>(1, 2));
mp.insert(make_pair(2, 2));
mp.insert(map<int, int>::value_type(3, 1));
if (mp.find(3) != mp.end())
{
cout << "找到了" << mp[3] << endl;
}


map自定义排序规则

利用仿函数,可以改变排序规则<br />对于自定义数据类型,map必须指定排序规则,同set容器

#include<iostream>
#include<string>
#include<map>
using namespace std;
class myCompare
{
public:
bool operator()(int v1, int v2)const
{
return v1 > v2;
}
};
int main()
{
map<int, int, myCompare>mp;
mp.insert(pair<int, int>(1, 2));
mp.insert(make_pair(2, 2));
mp.insert(map<int, int>::value_type(3, 1));
for (map<int, int, myCompare>::iterator it = mp.begin(); it != mp.end(); it++)
{
cout << it->first << " " << it->second << endl;
}
return 0;
}

标签:map,multimap,insert,STL,mp,key,pair,PrintMap
From: https://blog.51cto.com/u_15791183/6047097

相关文章

  • 提供一个方法,遍历获取HashMap<String,String>中的所有value,并存放在list中返回,考虑泛
    publicList<String>getValueList(HashMap<String,String>map){ArrayList<String>valueList=newArrayList<>();Collection<String>values=map......
  • HashMap长度为什么是2的幂
    虽然hash值很多,范围很大,但是内存存不了那么大的数组,所以取hash的散列值的时候,需要用hash值,除以数组长度取余数。又由于取余数(%)的性能不如与运算(&),所以想用与运算来代替取余......
  • MyBatis之ResultMap简介,关联对象
    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟re......
  • Codeforces Round #849 (Div. 4)-F. Range Update Point Query-STL的应用(bushi)
    题目:https://codeforces.com/problemset/problem/1791/F看完差点想写线段树了(bushi)但其实用set维护一下位置就行注:lower_bound(x)找的是第一个大于等于x的元素(的地址),以......
  • Webpack 中使用source map 在开发过程中进行调试
    我们都知道webpack在打包的时候会将源代码打包成一个bundle文件,bundle文件就是经过了loader转换,还有webpack的一些插件处理,以及webpack构建过程中的一些转换,最后会生成一个......
  • 移动端IOS配置whistle代理
    1.官网安装whistle地址https://wproxy.org/whistle/install.html2.移动端使用whistle额外说明移动端需要先下载本地启动的w2startwhistle证书;打开本地电脑的代理地......
  • Mybatis 复杂对象resultMap的使用
    目录mybatis复杂对象resultMap下面是resultMap的定义普通属性省略说明select相关配置Model代码resultMap处理复杂映射问题Ⅰ多对一查询:学生——老师(1)创......
  • 解决resultMap映射数据错误的问题
    目录resultMap映射数据错误解决方案【报错】resultMap认知错误附图(修改过后的) resultMap映射数据错误mapper文件使用了resultMap进行一对多关系映射,不管怎么......
  • threejs 第四十六用 ImageBitmapLoader
    threejs交流群511163089这东西。。今天研究worker读url那。发现这个可以用这种bitmap可以读来传给主线程然后创建CanvasTextureloader.load(url,function(res){self.post......
  • OpenGL API学习记录glMapBuffer
    除了glBufferSubData还有MapBuffer这种方式来修改数据floatdata[]={0.5f,1.0f,-0.35f...};glBindBuffer(GL_ARRAY_BUFFER,buffer);//获取指针void*ptr=glMapB......