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

C++之map

1、标准库的map类型

2、插入数据

#include <map>
#include <string>
#include <iostream>using namespace std;int main() {map<string, int> mapTest;// 插入到map容器内部的元素是默认按照key从小到大来排序// key类型一定要重载小于号<运算符mapTest["aaa"] = 100;  // int& operator[](const string& index);mapTest["eee"] = 500;  // 可以重新赋值,但是下面的用insert方法就不能重新赋值mapTest.insert(map<string, int>::value_type("bbb", 200));mapTest.insert(map<string, int>::value_type("bbb", 2000));mapTest.insert(pair<string, int>("ccc", 300));mapTest.insert(pair<string, int>("ccc", 3000));mapTest.insert(make_pair("ddd", 400));mapTest.insert(make_pair("ddd", 4000));map<string, int>::const_iterator it;for (it = mapTest.begin(); it != mapTest.end(); ++it){cout << it->first << " " << it->second << endl;}return 0;
}// 输出
aaa 100
bbb 200
ccc 300
ddd 400
eee 500

3、查找与修改

#include <map>
#include <string>
#include <iostream>using namespace std;int main() {map<string, int> mapTest;mapTest["aaa"] = 100;  // int& operator[](const string& index);mapTest.insert(map<string, int>::value_type("bbb", 200));mapTest.insert(pair<string, int>("ccc", 300));mapTest.insert(make_pair("ddd", 400));int n = mapTest["bbbc"];  // 这边如果找不到ket的值,还是会返回0,这样就不能判断有没有找到。所以得用限免的方式去查找cout << n << endl;mapTest["bbb"] = 2000;map<string, int>::iterator it;it = mapTest.find("ccc");if (it != mapTest.end()){it->second = 3000;}else{cout << "not found" << endl;}//    map<string, int>::const_iterator it;for (it = mapTest.begin(); it != mapTest.end(); ++it){cout << it->first << " " << it->second << endl;}return 0;
}// 输出
0
aaa 100
bbb 2000
bbbc 0
ccc 3000
ddd 400

4、删除

#include <map>
#include <string>
#include <iostream>using namespace std;int main() {map<string, int> mapTest;// 插入到map容器内部的元素是默认按照key从小到大来排序// key类型一定要重载小于号<运算符mapTest["aaa"] = 100;  // int& operator[](const string& index);mapTest.insert(map<string, int>::value_type("bbb", 200));mapTest.insert(pair<string, int>("ccc", 300));mapTest.insert(make_pair("ddd", 400));mapTest.erase("bbb");map<string, int>::const_iterator it;it = mapTest.find("ccc");if (it != mapTest.end()){mapTest.erase(it);}for (it = mapTest.begin(); it != mapTest.end(); ++it){cout << it->first << " " << it->second << endl;}return 0;
}// 输出
aaa 100
ddd 400

相关文章:

  • OVF(Open Virtualization Format)或OVA(Open Virtual Appliance)格式有什么区别
  • OpenGL 和 DirectX 矩阵 乘法 左乘和右乘,glm和DirectXMath。 OpenGL用列矩阵 在 glsl中反转矩阵
  • C语言(数据存储)
  • 计算机网络学习实践:模拟PPP协议验证虚拟局域网(VLAN)
  • 【人工智能Ⅱ】实验8:生成对抗网络
  • 做外贸,怎么选国外服务器?
  • “神经网络之父”和“深度学习鼻祖”Geoffrey Hinton
  • 字节裁员!开启裁员新模式。。
  • 鸿蒙开发接口资源调度:【@ohos.backgroundTaskManager (后台任务管理)】
  • uni微信小程序editor富文本组件如何插入图片
  • Linux离线一键安装Docker及docker-compose环境
  • Double Add
  • linux中最基础使用的命令
  • iOS AVFoundation 音视频源码分享
  • LNMP 环境下使用 Zstd 压缩优化网站备份脚本
  • 收藏网友的 源程序下载网
  • __proto__ 和 prototype的关系
  • css选择器
  • js操作时间(持续更新)
  • js对象的深浅拷贝
  • Laravel 实践之路: 数据库迁移与数据填充
  • maven工程打包jar以及java jar命令的classpath使用
  • Node + FFmpeg 实现Canvas动画导出视频
  • Node 版本管理
  • Redux系列x:源码分析
  • Spark学习笔记之相关记录
  • windows下mongoDB的环境配置
  • 成为一名优秀的Developer的书单
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 多线程事务回滚
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 用jquery写贪吃蛇
  • 用Python写一份独特的元宵节祝福
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • ​14:00面试,14:06就出来了,问的问题有点变态。。。
  • ​linux启动进程的方式
  • #如何使用 Qt 5.6 在 Android 上启用 NFC
  • #数据结构 笔记一
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (2)STL算法之元素计数
  • (BAT向)Java岗常问高频面试汇总:MyBatis 微服务 Spring 分布式 MySQL等(1)
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (NSDate) 时间 (time )比较
  • (二)正点原子I.MX6ULL u-boot移植
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (附源码)计算机毕业设计高校学生选课系统
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (三分钟)速览传统边缘检测算子
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (转)清华学霸演讲稿:永远不要说你已经尽力了
  • .net dataexcel 脚本公式 函数源码
  • .Net 基于.Net8开发的一个Asp.Net Core Webapi小型易用框架
  • .NET开源、简单、实用的数据库文档生成工具
  • .net中生成excel后调整宽度