• 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

  • 相关阅读:
    HCIP第十八天——链路聚合,VRRP,IPV6
    【Java面向对象】封装的认识与实现
    8.10 哈希表简单&中等 202 Happy Number 36 Valid Sudoku
    用于非线性多载波卫星信道的多输入多输出符号速率信号数字预失真器DPD(Matlab代码实现)
    C++类型转换
    音视频基础知识
    【吴恩达机器学习笔记】十、支持向量机
    整理Meta GDC 2024 上关于XR、空间计算相关的分享
    【Java 基础篇】Java多线程编程详解:线程创建、同步、线程池与性能优化
    嵌入式Linux入门-Framebuffer应用编程在Linux系统下画个点
  • 原文地址:https://blog.csdn.net/m0_67391683/article/details/126317963