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

c++用map,创建类似于python中的字典

1.创建

#include <map>
#include <string>
#include <iostream>using namespace std;
int main() {/*using std::map;using std::string;using std::cout;*/map<string, string> myMap1 = {{"Name", "ClearLove"},{"Gender", "Male"},{"Position", "Jungle"},{"ID", "1"}};map<string, string> myMap2;myMap2["Name"] = "TheShy";myMap2["Gender"] = "Male";myMap2["Position"] = "Top";myMap2["ID"] = "2";map<string, string> myMap3(myMap2.begin(), myMap2.end());myMap3["ID"] = "3";map<string, string> myMap4(myMap3);myMap4["ID"] = "4";map<string, string> mapArr[4] = { myMap1, myMap2, myMap3, myMap4 };map<string, string>::iterator iter;//这种方法能够取出保存在map容器里面的数据for (int iMap = 0; iMap < 4; iMap++) {cout << "Map " << iMap + 1 << " of 4\n";for (iter = mapArr[iMap].begin(); iter != mapArr[iMap].end(); iter++) {cout << "key: " << iter->first << " , value: " << iter->second << ".\n";}}return 0;
}#include <map>
#include <string>
#include <iostream>using namespace std;
int main() {map<int, int> dict1;dict1[0] = 1;dict1[1] = 2;dict1[10] = 10;map<int, int>::iterator iter;iter = dict1.begin();while (iter != dict1.end()) {cout << iter->first << " : " << iter->second << endl;iter++;}//取出值的方法int a = dict1[0];cout << a << endl;cout << "usually:" << dict1.at(0) << endl;map< int,int >::iterator myIter = dict1.find(0);cout << myIter->first << "key<-::->value" << myIter->second << endl;}

2.修改和增加

#include <map>
#include <string>
#include <iostream>
int main()
{map<int , string>dict1;for(int i = 0;i<=4;i++){dict1[i]="hello world";cout<<dict1[i]<<endl;}cout<<"++++++++++++++++++++"<<endl;//修改 string a;a = "12345678";dict1[0] =a;for(int i = 0;i<=4;i++){cout<<dict1[i]<<endl;}dict1[5]="12366";cout<<dict1[5]<<endl; //增加 cout<<"++++++++insert++++++++++++"<<endl;dict1.insert(pair<int,string>(9,"insert")) ;cout<<dict1[9]<<endl;//删除cout<<"+++++++++erase+++++++++++"<<endl;dict1.erase(2);for(int i = 0;i<=9;i++){cout<<dict1[i]<<endl;}}
http://www.lryc.cn/news/229075.html

相关文章:

  • VuePress介绍及使用指南
  • Spring-Security前后端分离权限认证
  • Django中Cookie和Session的使用
  • 云原生周刊:KubeSphere 3.4.1 发布 | 2023.11.13
  • 逐帧动画demo
  • Mongodb 中,与索引相关的监控指标
  • 图论14-最短路径-Dijkstra算法+Bellman-Ford算法+Floyed算法
  • OpenCV 实现透视变换
  • ChinaSoft 论坛巡礼|开源软件供应链论坛
  • VUE 组合式API
  • 尝试使用php给pdf添加水印
  • ubuntu上安装edge浏览器
  • 动态切换 Spring Boot 打包配置:使用 Maven Profiles 管理 JAR 和 WAR
  • 微信小程序使用阿里巴巴矢量图标
  • 使用JAVA pdf转word
  • 成都瀚网科技有限公司抖音带货的正规
  • windows服务器热备、负载均衡配置
  • samba服务器搭建 挂载远程目录 常用配置参数介绍
  • Ansible命令使用
  • element 周选择器el-date-picker
  • No200.精选前端面试题,享受每天的挑战和学习
  • 前端面试之事件循环
  • sass 封装媒体查询工具
  • 眼科动态图像处理系统使用说明(2023-8-11 ccc)
  • 国际阿里云:提高CDN缓存命中率教程!!!
  • 关于“谈谈你对 ES 的理解”
  • Element-Ui el-table 动态添加行
  • 深度学习 大数据 股票预测系统 - python lstm 计算机竞赛
  • vue v-model
  • springboot整合ELK