C++find_if算法

C++find_if算法

功能描述:
按条件查找元素

函数原型:

find_if(iterator beg, iterator end, _Pred);
//按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

//beg开始迭代器

//end结束迭代器

//_Pred函数或者谓词(返回bool类型的仿函数)

代码示例:

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
//常用查找算法find_if
//1.查找内置数据类型
class GreaterFive
{
public:
       bool operator()(int val)
       {
              return val > 5;
       }
};
void test01()
{
       vector<int>v;
       for (int i = 0; i < 10; i++)
       {
              v.push_back(i);
       }
       vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
       if (it == v.end())
       {
              cout << "没有找到" << endl;
       }
       else
       {
              cout << "找到大于5的数字为:" << *it << endl;
       }
}
//2.查找自定义数据类型
class Person
{
public:
       Person(string name, int age)
       {
              this->m_Name = name;
              this->m_Age = age;
       }
       string m_Name;
       int m_Age;
};
class Greater20
{
public:
       bool operator()(Person &p)
       {
              return p.m_Age > 20;
       }
};
void test02()
{
       vector<Person>v;
       //创建数据
       Person p1("aaa", 10);
       Person p2("bbb", 20);
       Person p3("ccc", 30);
       Person p4("ddd", 40);
       v.push_back(p1);
       v.push_back(p2);
       v.push_back(p3);
       v.push_back(p4);
       //找年龄大于20的人
       vector<Person>::iterator it= find_if(v.begin(), v.end(), Greater20());
       if (it == v.end())
       {
              cout << "没有找到" << endl;
       }
       else
       {
              cout << "找到姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
       }
}
int main()
{
       test01();
       test02();
       system("pause");
       return 0;
}
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++中,`std::find_if` 是一个算法函数,用于在给定范围内查找满足指定条件的元素。该函数需要三个参数:范围的起始迭代器、范围的结束迭代器和一个谓词(条件)函数。 以下是 `std::find_if` 的函数签名: ```cpp template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, InputIt last, UnaryPredicate p); ``` 其中: - `InputIt` 是迭代器类型,表示范围的起始和结束位置。 - `UnaryPredicate` 是一个可调用对象,用于确定元素是否满足条件。它接受一个参数并返回一个 `bool` 类型的结果。 以下是一个使用 `std::find_if` 函数的示例代码,演示如何查找一个数组中第一个大于 5 的元素: ```cpp #include <iostream> #include <vector> #include <algorithm> bool isGreaterThan5(int num) { return num > 5; } int main() { std::vector<int> numbers = {2, 4, 6, 8, 10}; auto it = std::find_if(numbers.begin(), numbers.end(), isGreaterThan5); if (it != numbers.end()) { std::cout << "First element greater than 5: " << *it << std::endl; } else { std::cout << "No element greater than 5 found." << std::endl; } return 0; } ``` 输出结果为: ``` First element greater than 5: 6 ``` 在这个示例中,我们定义了一个 `isGreaterThan5` 函数作为谓词,用于确定元素是否大于 5。然后,我们使用 `std::find_if` 函数在 `numbers` 向量中查找第一个满足条件的元素,并将结果存储在迭代器 `it` 中。最后,我们输出找到的元素(如果存在)或相应的提示信息。 你可以根据自己的需求编写不同的谓词函数来满足不同的查找条件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黑马金牌编程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值