C++笔记之map使用

头文件#include<map>.

map<string,int> m,存入的是键值对,自动按键值升序排列。

插入:通过关键字或insert,五种方式。

访问:m[5]为关键字5对应的值;
           迭代器it->first,it->second. 

查找:it=m.find(5),返回关键字5对应的迭代器,若未找到,返回m.end()

删除:m.erase(5),删除关键字为5的元素;
           m.erase(it),删除迭代器指定的元素
      
判空:m.empty(),若为空,返回1;若不空,返回0。

清空:m.clear(). 

具体用法均标注在代码中。

#include<iostream>
#include<map>

using namespace std;

int main(){

//1.键为int,值为string 
	map<int,string> m1;
	
//插入 
	m1[1]="aaa"; //直接 
	m1.insert(make_pair(2,"bbb")); //make_pair
	m1.insert(pair<int,string>(3,"ccc")); //pair
	pair<int,string> v(4,"ddd"); m1.insert(v);//pair,insert
	m1.insert(map<int,string>::value_type(5,"eee"));//map::value_type
	
	//输出
	cout<<"元素个数:"<<m1.size()<<endl;   //大小 
	map<int,string>::iterator it1;
	for(it1=m1.begin();it1!=m1.end();it1++){
		cout<<it1->first<<' '<<it1->second<<endl;
	} 
	cout<<endl;
	
//访问	
	cout<<"关键字3对应的值为:"<<m1[3]<<endl; //通过键值
	it1=m1.begin();
	cout<<it1->first<<' '<<it1->second<<endl; //通过迭代器,访问第一个元素 
	cout<<endl;
	
//查找
	it1=m1.find(4);//返回查找的关键字对应的迭代器,若未找到,返回m1.end() 
	if(it1!=m1.end()) cout<<"找到关键字4对应的值为:"<<it1->second<<endl;
	cout<<endl;
	
//删除
	it1=m1.begin();
	m1.erase(it1);//根据迭代器,删除第一个
	m1.erase(4);//根据键值,删除第四个
	
	//输出
	cout<<endl<<"元素个数:"<<m1.size()<<endl; 
	for(it1=m1.begin();it1!=m1.end();it1++){
		cout<<it1->first<<' '<<it1->second<<endl;
	} 
	cout<<endl;
	 
//判空、清空 
	cout<<"map空吗:"<<m1.empty()<<endl;
	m1.clear();
	cout<<"map空吗:"<<m1.empty()<<endl;
/*---------------------------------------------------------------------------------------------*/
cout<<endl<<endl<<"=============================================================="<<endl<<endl;
/*---------------------------------------------------------------------------------------------*/
//2.键为string,值为int 
	map<string,int> m2;

//插入
	m2["aaa"]=1;
	m2.insert(make_pair("bbb",2));
	m2.insert(pair<string,int>("ccc",3));
	pair<string,int> v2("ddd",4); m2.insert(v2);	
	m2.insert(map<string,int>::value_type("eee",5));
	
	//输出
	cout<<"元素个数:"<<m2.size()<<endl;   //大小 
	map<string,int>::iterator it2;
	for(it2=m2.begin();it2!=m2.end();it2++){
		cout<<it2->first<<' '<<it2->second<<endl;
	} 
	cout<<endl; 
 
//访问	
	cout<<"关键字ccc对应的值为:"<<m2["ccc"]<<endl; //通过键值
	it2=m2.begin();
	cout<<it2->first<<' '<<it2->second<<endl; //通过迭代器,访问第一个元素 
	cout<<endl; 

//查找
	it2=m2.find("ddd");//返回查找的关键字对应的迭代器,若未找到,返回m2.end() 
	if(it2!=m2.end()) cout<<"找到关键字ddd对应的值为:"<<it2->second<<endl;
	cout<<endl;
 
//删除
	it2=m2.begin();
	m2.erase(it2);//根据迭代器,删除第一个
	m2.erase("ddd");//根据键值,删除第四个
 
 	//输出
	cout<<endl<<"元素个数:"<<m2.size()<<endl; 
	for(it2=m2.begin();it2!=m2.end();it2++){
		cout<<it2->first<<' '<<it2->second<<endl;
	} 
	cout<<endl;

//判空、清空 
	cout<<"map空吗:"<<m2.empty()<<endl;
	m2.clear();
	cout<<"map空吗:"<<m2.empty()<<endl; 
 
	return 0;
} 

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

禺垣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值