#include <stdio.h>
#include <deque>
#include <algorithm>
using namespace std;
class A
{
public:
int m_nA;
void print()
{
printf("%d\n", m_nA);
}
bool operator==(const A& a)
{
return a.m_nA == m_nA;
}
};
class A_filter
{
public:
A_filter(const A& a)
{
this->a.m_nA = a.m_nA;
}
bool operator()(const A& a)
{
return a.m_nA == a.m_nA;
}
A a;
};
class B
{
public:
void search1(const A& a)
{
m_nIter = std::find(m_nList.begin(), m_nList.end(), a);
m_nIter->print();
}
void search2(const A& a)
{
m_nIter = std::find_if(m_nList.begin(), m_nList.end(), A_filter(a));
m_nIter->print();
}
deque<A> m_nList;
deque<A>::iterator m_nIter;
};
int main()
{
A a1, a2, a3;
B b;
a1.m_nA = 1;
a2.m_nA = 2;
a3.m_nA = 3;
b.m_nList.push_back(a1);
b.m_nList.push_back(a2);
b.m_nList.push_back(a3);
b.search1(a1);
b.search1(a3);
return 0;
}
stl的find、find_if简单demo
最新推荐文章于 2023-05-10 06:00:00 发布