1 Star 0 Fork 1

llongger/Code_C++

forked from GodOuO/Code_C++ 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
80.find_if.cpp 1.36 KB
一键复制 编辑 原始数据 按行查看 历史
GodOuO 提交于 2023-03-15 04:08 . Train
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
//常用查找算法 find_if
class Person
{
public:
string m_Name;
int m_Age;
Person(string name,int age){
this->m_Name = name;
this->m_Age = age;
}
};
class Greater5
{
public:
bool operator()(int a){
return a>5;
}
};
class Greater51
{
public:
bool operator()(const Person &p){
return p.m_Age>51;
}
};
//查找内置数据类型
void test01(){
vector<int> v;
for (int i = 0; i < 10; i++)
v.push_back(i+1);
vector<int>::iterator it = find_if(v.begin(),v.end(),Greater5());
if(it == v.end())
cout<<"Not Found!"<<endl;
else{
cout<<"Find elem which is bigger than 5 is : "<< *it<<endl;
}
}
//查找自定义数据类型
void test02(){
vector<Person> v;
Person p1("GodOuO",51);
Person p2("Wendy",384);
Person p3("Fury",126);
Person p4("P4",304);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//找年龄 >20 elem
vector<Person>::iterator it = find_if(v.begin(),v.end(),Greater51());
if (it == v.end())
cout<<"Not Found the elem > 51 "<<endl;
else{
cout<<"Find it _Name : "<<it->m_Name<<"\tAge : "<<it->m_Age<<endl;
}
}
int main(){
//test01();
test02();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/llongger/codeCpp.git
[email protected]:llongger/codeCpp.git
llongger
codeCpp
Code_C++
master

搜索帮助