当前位置: 首页 > 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

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

相关文章:

  • 【量算分析工具-方位角】GeoServer改造Springboot番外系列六
  • 【机器学习】机器学习与大模型在人工智能领域的融合应用与性能优化新探索
  • 上传图片并显示#Vue3#后端接口数据
  • 音视频开发14 FFmpeg 视频 相关格式分析 -- H264 NALU格式分析
  • Qt学习记录(15)数据库
  • c++常用设计模式
  • 【动手学深度学习】softmax回归从零开始实现的研究详情
  • MySQL:MySQL执行一条SQL查询语句的执行过程
  • 解决Python导入第三方模块报错“TypeError: the first argument must be callable”
  • 在python中连接了数据库后想要在python中通过图形化界面显示数据库的查询结果,请问怎么实现比较好? /ttk库的treeview的使用
  • OZON的选品工具,OZON选品工具推荐
  • 营销方案撰写秘籍:包含内容全解析,让你的方案脱颖而出
  • 如何制作一本温馨的电子相册呢?
  • 485通讯网关
  • Anaconda中的常用科学计算工具
  • Java 中BigDecimal传到前端后精度丢失问题
  • 在Linux/Ubuntu/Debian上安装TensorFlow 2.14.0
  • 多语言for循环遍历总结
  • python API自动化(Jsonpath断言、接口关联及加密处理)
  • C++入门5——C/C++动态内存管理(new与delete)
  • leetcode 743.网络延时时间
  • MATLAB导入导出Excel的方法|读与写Excel的命令|附例程的github下载链接
  • 【第4章】SpringBoot实战篇之登录优化(含redis使用)
  • 数据结构:详解二叉树(树,二叉树顺序结构,堆的实现与应用,二叉树链式结构,链式二叉树的4种遍历方式)
  • HarmonyOS-9(stage模式)
  • RestTemplate代码内部访问RESTful服务的客户端工具
  • Flutter 中的 SliverLayoutBuilder 小部件:全面指南
  • 家政预约小程序11新增预约
  • AI雷达小程序个人名片系统源码 PHP+MYSQL组合开发 带完整的安装代码包以及搭建教程
  • Kafka生产者消息异步发送并返回发送信息api编写教程