• c++中删除map元素的三种方式


    //删除map元素的三种方式
    //by 鸟哥 
    #include 
    #include 
    using namespace std;
    
    void printMap(const map& students)
    {
        for (auto ii = students.begin() ; ii != students.end() ; ii++)
        {
            cout <<"姓名:"<< ii->first
            << " 年龄: " << ii->second
            << endl;
        }
        cout< students;
        students["建国"] = 12;
        students["爱华"] = 11;
        students["从从"] = 13;
        students["丛丛"] = 11;
        students["北京"] = 9;
        cout<<"原map:"<::iterator iter=students.find("建国");
        students.erase(iter);
        cout<<"删除建国后:"<
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    运行结果:

    原map:
    姓名:丛丛 年龄: 11
    姓名:从从 年龄: 13
    姓名:北京 年龄: 9
    姓名:建国 年龄: 12
    姓名:爱华 年龄: 11

    删除 丛丛 后:
    姓名:从从 年龄: 13
    姓名:北京 年龄: 9
    姓名:建国 年龄: 12
    姓名:爱华 年龄: 11

    删除第一个元素后:
    姓名:北京 年龄: 9
    姓名:建国 年龄: 12
    姓名:爱华 年龄: 11

    删除建国后:
    姓名:北京 年龄: 9
    姓名:爱华 年龄: 11

  • 相关阅读:
    SpringCloud和SpringBoot在调Feign传文件时的异常汇总及解决办法
    【3568开发板教程上新】Android11移植开发教程升级来袭!
    [机缘参悟-32]:鬼谷子-抵巇[xī]篇-面对危险与问题的五种态度
    Transformer预测 | Python实现基于Transformer的股票价格预测(tensorflow)
    三大数据库 sequence 之华山论剑 (上篇)
    解说天下之操作系统
    实现一个深克隆
    Java扩展Nginx之五:五大handler(系列最核心)
    C++ Qt开发:MdiArea多窗体组件
    Linux命令
  • 原文地址:https://blog.csdn.net/m0_67391683/article/details/126317963