map find find_if用法

本文通过两个示例介绍了C++标准模板库(STL)中find_if与find函数的应用方法。find_if用于查找满足特定条件的第一个元素,而find则用于查找指定值的第一个匹配项。示例涉及map容器及迭代器的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;

bool equal(pair<int,int> data, int target)
{
	if (data.second == target)
	{
		return   true;
	} 
	else
	{
		return   false;
	}
}

int main()
{

	//find_if用法;
	map<int,int>  mymap;
	map<int,int>::iterator iter_find;
	mymap[1] = 10;
	mymap[2] = 20;
	mymap[3] = 30;
	mymap[4] = 40;

	iter_find = find_if(mymap.begin(), mymap.end(), bind2nd(ptr_fun(equal),50));
	if (iter_find != mymap.end())
	{
		cout<<" find!!!! "<<endl;
	}
	else
	{
		cout<< "Not find!!!! "<<endl;	
	}

	//find用法;
	map<char,int> mymap2;
	map<char,int>::iterator it;

	mymap2['a'] = 50;
	mymap2['b'] = 100;
	mymap2['c'] = 150;
	mymap2['d'] = 200;

	it = mymap2.find('b');
	cout<<it->second<<endl;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值