当前位置: 首页 > news >正文

C++ map和multimap的键查找和值查找以及删除操作

C++的map和multimap本质都是排序的平衡二叉树。其中不同的点在于

  • map——key是唯一的。
  • multimap——key是不唯一的。

另外需要提及的一点是它们的删除操作,在删除某个迭代器的时候会导致迭代器失效。下面的代码主要介绍几个特殊的查找函数:

  • find——已知key查找map或者multimap中的第一个满足条件的值。
  • find_if——已知起始迭代器,终止迭代器,bool表达式的第一个满足表达式的值。(该函数来自algorithm包)
  • lower_bound——已知key,查找>=key的第一个迭代器
  • upper_bound——已知key,查找>key的第一个迭代器
  • equal_range——已知key,查找key的起始迭代器和终止迭代器

代码演示:

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
bool cmp(const pair<int,int> &elem){
    return elem.second==3;
}
int main() {
    //赋值
    map<int,int> coll={{1,7},{2,4},{3,2},{4,3},{5,6},{6,1},{7,3}};
    //查找键值为3的元素
    auto posKey=coll.find(3);
    if(posKey!=coll.end()){
        cout<<"key-3:("<<posKey->first<<","<<posKey->second<<")"<<endl;
    }
    cout<<"======================"<<endl;
    //查找值为3的元素
    auto posVal=find_if(coll.begin(),coll.end(),cmp);
    if(posVal!=coll.end()){
        cout<<"value-3:("<<posVal->first<<","<<posVal->second<<")"<<endl;
    }
    cout<<"======================"<<endl;
    //给multimap赋值
    multimap<int,string> multicoll={{1,"h"},{1,"i"},{1,"j"},{3,"world"},{4,"six"},{4,"seven"},{6,"what"},{6,"yy"},{7,"what"},{8,"niu"}};
    //测试查找key为1的上下元素
    pair<multimap<int,string>::iterator,multimap<int,string>::iterator> ret=multicoll.equal_range(1);
    for(auto pos=ret.first;pos!=ret.second;++pos){
        cout<<"key-1:("<<pos->first<<","<<pos->second<<")"<<endl;
    }
    cout<<"======================"<<endl;
    //找出<=4的元素进行删除
    auto pos1=multicoll.lower_bound(4);
    auto pos2=multicoll.begin();
    while(pos2!=pos1){
        if(pos2->first<=4) {
            pos2 = multicoll.erase(pos2);//先缓存再删除
            continue;//这一步很重要,删除完之后不要pos2++
        }
        pos2++;
    }
    for(auto pos3=multicoll.begin();pos3!=multicoll.end();++pos3){
        cout<<"key:("<<pos3->first<<","<<pos3->second<<")"<<endl;
    }
    cout<<"======================"<<endl;
    //删除>=6的元素
    auto pos4=multicoll.upper_bound(4);
    while(pos4!=multicoll.end()){
        if(pos4->first>=4) {
            pos4 = multicoll.erase(pos4);//先缓存再删除
            continue;//这一步很重要,删除完之后不要pos2++
        }
        pos4++;
    }
//    查看删除后的mutlicoll
    for(auto pos3=multicoll.begin();pos3!=multicoll.end();++pos3){
        cout<<"key:("<<pos3->first<<","<<pos3->second<<")"<<endl;
    }
    return 0;
}

运行结果

key-3:(3,2)
======================
value-3:(4,3)
======================
key-1:(1,h)
key-1:(1,i)
key-1:(1,j)
======================
key:(4,six)
key:(4,seven)
key:(6,what)
key:(6,yy)
key:(7,what)
key:(8,niu)
======================
key:(4,six)
key:(4,seven)

进程已结束,退出代码 0

相关文章:

  • 学习make和CMake的经典文档和视频
  • Go语言关于PorstgreSQL数据库的驱动-pg
  • linux调式命令
  • Bash快捷键
  • PostgreSQL插件汇总
  • ulimit详解
  • 数据库连接接口(驱动)
  • pgbench和sysbench初次压测PG集群
  • Patroni-2.0.0(Postgresql集群高可用方案)说明书
  • 跟我一起写 Makefile(二)
  • 发现发现返回一条记录的datareader的新用法
  • 看完《浅析FTP的工作原理》一文, 记录小小,感想多多!~
  • 在首页去掉了其他分类的随笔标题的显示
  • Lose a bit !
  • 有用的国外开源项目网址
  • CAP 一致性协议及应用解析
  • Docker下部署自己的LNMP工作环境
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • rabbitmq延迟消息示例
  • React 快速上手 - 07 前端路由 react-router
  • SpiderData 2019年2月13日 DApp数据排行榜
  • VUE es6技巧写法(持续更新中~~~)
  • 给新手的新浪微博 SDK 集成教程【一】
  • 深度学习中的信息论知识详解
  • 算法系列——算法入门之递归分而治之思想的实现
  • 在weex里面使用chart图表
  • ​水经微图Web1.5.0版即将上线
  • (1)bark-ml
  • (阿里云万网)-域名注册购买实名流程
  • (二)构建dubbo分布式平台-平台功能导图
  • (二十三)Flask之高频面试点
  • (企业 / 公司项目)前端使用pingyin-pro将汉字转成拼音
  • (全注解开发)学习Spring-MVC的第三天
  • (万字长文)Spring的核心知识尽揽其中
  • (原創) 系統分析和系統設計有什麼差別? (OO)
  • (转)nsfocus-绿盟科技笔试题目
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .net core 源码_ASP.NET Core之Identity源码学习
  • @RequestBody与@ResponseBody的使用
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • [20150629]简单的加密连接.txt
  • [AR]Vumark(下一代条形码)
  • [GN] Vue3.2 快速上手 ---- 核心语法2
  • [JAVA设计模式]第二部分:创建模式
  • [Jquery] 实现鼠标移到某个对象,在旁边显示层。
  • [leetcode] Balanced Binary Tree
  • [leetcode] 四数之和 M
  • [LeetCode]—Rotate Image 矩阵90度翻转
  • [Machine Learning][Part 8]神经网络的学习训练过程
  • [MTK]安卓8 ADB执行ota升级
  • [Python]面向对象基础
  • [Python3网络爬虫开发实战] 5.3-非关系型数据库存储
  • [rancher] rancher部署和使用的一些思考
  • [Unity Sentis] Unity Sentis 详细步骤工作流程
  • [Vue]路由传参 命名路由