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

4 篇文章 0 订阅
2 篇文章 0 订阅

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
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
multimap是一个允许元素重复的关联容器,它的内部实现也是使用红黑树。它可以在插入时按照特定的顺序进行排序,并且可以包含相同的。在C++中,我们可以使用multimap来存储一组对,其中可以重复。例如,我们可以使用multimap来存储不同班级的学生名字,每个班级可能有多个相同名字的学生。使用multimap时,我们可以通过迭代器遍历其中的元素。 unordered_map是一个使用哈希表实现的关联容器,它不允许重复的。与multimap不同,unordered_map不会对元素进行排序。它的插入和查找操作的时间复杂度都是O(1)。unordered_map适用于需要快速查找和插入元素的情况。例如,我们可以使用unordered_map来存储员工的姓名和工资,通过姓名快速查找对应的工资。使用unordered_map时,我们可以使用迭代器遍历其中的元素。 所以,如果你需要允许重复的并且需要按照特定顺序来存储元素,你可以使用multimap。如果你不需要重复的并且希望在常量时间内进行查找和插入操作,你可以使用unordered_map。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++ STL multimap与unordered_map](https://blog.csdn.net/yao_hou/article/details/107419196)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++ STL库中的multimap和unordered_multimap](https://blog.csdn.net/Woosual/article/details/107884396)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值