(map&C++)(三、查找)find

查找find

判断key值是否存在(1),通过find查看

	map<int ,string> m;
   	m[132] = "456";m[456]="789";m[789]="123";
   	map<int,string>::iterator iter = m.find(132);
   	if(iter != m.end()){
		cout << iter->first << "->" << iter->second << endl;
	}

结果:

在这里插入图片描述

判断key值是否存在(2),通过count函数查看

	map<int ,string> m;
   	m[132] = "456";m[456]="789";m[789]="123";
   	if(m.count(123)){
		cout << "123的key已经存在";
	}else{
		m[123] = "147";
	}
   	for(map<int,string>::iterator it = m.begin(); it != m.end(); it++){
   		cout<<it->first<<"->"<<it->second<<endl;
	}

结果:

在这里插入图片描述

发布了42 篇原创文章 · 获赞 40 · 访问量 960

猜你喜欢

转载自blog.csdn.net/weixin_44635198/article/details/104543218