#include <map>
#include <iostream>
using namespace std;
int main()
{
map< string,int > a;
cout<<"插入:"<<endl;
a["April"]=112;//插入
cout<<a["April"]<<endl;
cout<<endl;
cout<<"查询:"<<endl;
map<string,int>::iterator pc;//查询一个元素是否在map里
pc=a.find("April");
if(pc==a.end()) cout<<"Not Find"<<endl;//如果找不到会自动返回“指向map尾部的迭代器”
else cout<<pc->second<<endl;//输出"April"对应的键值
cout<<endl;
cout<<"遍历"<<endl;
a["June"]=1;//遍历
a["July"]=2;
map<string,int>::iterator i;
for(i=a.begin(); i!=a.end(); i++)//不能等于a.end()
{
cout<<i->second<<endl;
}
cout<<endl;
cout<<"判断map是否是空集"<<endl;
cout<<a.empty()<<endl;//判断map是否是空集
cout<<endl;
cout<<"交换两个map的元素 "<<endl;
map<int ,int> b;//交换两个map的元素
map<int ,int> c;
c[1]=1;
c[2]=2;
b[1]=1000;
swap(b,c);
map<int,int> ::iterator j;
for(j=b.begin();j!=b.end();j++)
{
cout<<j->second<<endl;
}
for(j=c.begin();j!=c.end();j++)
{
cout<<j->second<<endl;
}
}
map用法
最新推荐文章于 2025-03-03 16:29:03 发布
本文详细介绍了使用C++标准库中的map容器进行各种操作的方法,包括插入、查询、遍历、判断是否为空以及交换两个map的内容等。通过具体的代码示例展示了如何实现这些功能。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
ACE-Step
音乐合成
ACE-Step
ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言
5万+

被折叠的 条评论
为什么被折叠?



