STL---map 和 multimap
2023-03-14 10:16:58 时间
#include <iostream> #include <map> #include <string> using namespace std; int main() { multimap<string,string>mulmap; multimap<string,string>::iterator p; typedef multimap<string,string>::value_type vt; typedef string s; mulmap.insert(vt(s("Tom "),s("is a student"))); mulmap.insert(vt(s("Tom "),s("is a boy"))); mulmap.insert(vt(s("Tom "),s("is a bad boy of blue!"))); mulmap.insert(vt(s("Jerry"),s(" is a student"))); mulmap.insert(vt(s("Jerry "),s("is a beatutiful girl"))); mulmap.insert(vt(s("DJ "),s("is a student"))); //输出初始化以后的多重映射mulmap: for(p=mulmap.begin();p!=mulmap.end();++p) cout<<(*p).first<<(*p).second<<endl; //检索并输出Jerry键所对应的所有的值 cout<<"find Jerry :"<<endl; p=mulmap.find(s("Jerry ")); while((*p).first=="Jerry ") { cout<<(*p).first<<(*p).second<<endl; ++p; } return 0; }
#include <iostream> #include <map> using namespace std; int main() { map<char,int> map1; map<char,int>::iterator mapIter; //char 是键的类型,int是值的类型 //下面是初始化,与数组类似 //也可以用map1.insert(map<char,int,less<char> >::value_type(''c'',3)); map1['c']=3; map1['z']=4; map1['a']=1; map1['k']=2; for(mapIter=map1.begin();mapIter!=map1.end();++mapIter) cout<<" "<<(*mapIter).first<<": "<<(*mapIter).second; //first对应定义中的char键,second对应定义中的int值 //检索对应于d键的值是这样做的: map<char,int>::iterator ptr; ptr=map1.find('k'); cout<<'\n'<<" "<<(*ptr).first<<" 键对应于值:"<<(*ptr).second; return 0; }
相关文章
- 生信星球day7-毽子
- RealWorldCtf2023-The_cult_of_8_bit详解
- nesbot/carbon 日期时间处理扩展包
- phpoffice/phpexcel 导出Excel表格数据
- Windows 系统 PhpStorm 2020无限试用30天
- 微擎安装模块时提示 Failed to connect to we7.rewlkj.com port 80: Timed out
- Sketch Fashion for mac(服装设计软件) v1.2.5中文版
- vue.js客服系统实时聊天项目开发(四)引入iconfont图标代码
- vue.js客服系统实时聊天项目开发(五)flex布局实现输入框区域
- SSH 公钥免密登录
- TP6.0 自定义日志驱动
- 【虹科新闻】虹科被评为“2022年广东省‘专精特新’中小企业”
- 【好玩儿的Docker项目】10分钟部署一个类似知乎的开源问答平台——Answer
- MLBSP - 我的世界轻量化基础开服包
- 【好玩儿的Docker项目】Hexo在线管理+Butterfly主题+自部署Twikoo评论系统
- Brain综述:基于脑网络引导的神经调控用于治疗癫痫
- XMind 2022 for mac (XMind思维导图) v22.11.3656中文版
- 振弦采集模块配置工具VMTool生成寄存器值
- Eigen 高维矩阵运算
- TP6.0 容器和依赖注入