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

C++:通过erase删除map的键值对

map是经常使用的数据结构,erase可以删除map中的键值对。

可以通过以下几种方式使用erase

1.通过迭代器进行删除

#include <iostream>
#include <map>
#include <string>
using namespace std;void pMap(const string& w, const auto& m)
{cout<<w<<endl;for(auto& [key, value] : m){cout<<key<<" = "<<value<<endl;}cout<<endl;
}int main() {map<string, string> m = {{"father", "ming"}, {"mother", "hong"}, {"boy", "xiaoxiao"}};pMap("init data", m);auto f = m.find("father");if(f != m.end()){m.erase(f);}pMap("after erase", m);return 0;
}

运行程序输出:

init data

boy = xiaoxiao

father = ming

mother = hong

after erase

boy = xiaoxiao

mo

http://www.lryc.cn/news/272569.html

相关文章:

  • 华为月薪25K的自动化测试工程师到底要会那些技能!
  • diffusers 源码待理解之处
  • 正则表达式 详解,10分钟学会
  • 【排序算法】归并排序与快速排序:深入解析与比较
  • 万字长文谈自动驾驶bev感知(一)
  • cfa一级考生复习经验分享系列(十七)
  • 机器人活动区域 - 华为OD统一考试
  • 三、HTML元素
  • 置顶> 个人学习记录一览
  • c++重载操作符
  • C# 如何读取Excel文件
  • Vue2面试题:说一下对vuex的理解?
  • elasticsearch系列五:集群的备份与恢复
  • 【Elasticsearch源码】 分片恢复分析
  • elasticsearch如何操作索引库里面的文档
  • opencv期末练习题(2)附带解析
  • 【Mybatis】深入学习MyBatis:高级特性与Spring整合
  • C语言与人生函数的对比,使用,参数详解
  • 机器人动力学一些笔记
  • Plantuml之甘特图语法介绍(二十八)
  • Docker support for NVIDIA GPU Accelerated Computing on WSL 2
  • SQL窗口函数大小详解
  • C#上位机与欧姆龙PLC的通信06---- HostLink协议(FINS版)
  • 认识SpringBoot项目中的Starter
  • ChatGPT 4.0真的值得花钱买入吗?
  • vue3对比vue2是怎样的
  • openGauss学习笔记-184 openGauss 数据库运维-升级-升级验证
  • [Verilog语言入门教程] Verilog 减法器 (半减器, 全减器, 加减共用)
  • 预编译仓库中的 Helm Chart
  • Python requests get和post方法发送HTTP请求