The STL find_if algorithm is used to find the eligible elements within the containerExamples are as follows:
1. The first way: to save the value to be compared in the constructor of an imitation function.
struct Stableinfor {
uint16 m_itableid;
}
First write The Imitation function:
Class Tablecomparefuctor
{public
:
tablecomparefuctor (const stableinfor tableinfo)
{
m_ Tableinfo = Tableinfo;
}
~ Tablecomparefuctor ()
{
}
bool Operator () (const stableinfor value)
{
if (value.m_ Itableid = = M_tableinfo.m_itableid)
{return
true;
}
else
{
return false;
}
}
Private:
stableinfor m_tableinfo;
Finally, call the FIND_IF algorithm: include the header file first:
#import "algorithm"
using namespace std;
Std::list<stableinfor> tableinforlist;
Std::list<stableinfor>::iterator iter_begin = tableinforlist.begin ();
Std::list<stableinfor>::iterator iter_end = Tableinforlist.end ();
Tablecomparefuctor Comparefuctor (tableinfo.m_tableinfor);
Std::list<stableinfor>::iterator iter_find = find_if (Iter_begin, Iter_end, comparefuctor);
if (iter_find!= iter_end)
{
tableinforlist.erase (iter_find);
}
The example above is to find the element that meets the criteria first and then remove it from the container.
2. The second approach: use of
Binary_function and
bind2nd
If you want to use the blind2nd adapter for your own imitation function, you must allow your own imitation function to inherit from Binary_function.
bind2nd represents the 2nd parameter of the binding, or it can be bind1st to bind the first argument.
The code example is as follows:
struct tablecomparefuctorwithadapter:public std::binary_function<stableinfor,stableinfor,bool>
{ Public
:
Tablecomparefuctorwithadapter () {}
~tablecomparefuctorwithadapter () {}
bool operator () (const Stableinfor value, const stableinfor fixedvalue) Const
{
if (Value.m_stableid = = Fixedvalue.m_stableid)
{ C10/>return true;
}
else
{
return false;
}}}
;
Finally, call the FIND_IF algorithm: include the header file first:
#import "algorithm"
using namespace std;
Std::vector<stableinfor>::iterator iter_begin = tableinforlist.begin ();
Std::vector<stableinfor>::iterator iter_end = Tableinforlist.end ();
Tablecomparefuctorwithadapter Comparefuctor;
Std::vector<stableinfor>::iterator iter_find =
find_if (
iter_begin,
iter_end,
std: : bind2nd (Comparefuctor, tableinfo.m_tableinfor)
);
if (iter_find!= iter_end)
{
tableinforlist.erase (iter_find);
}
This example also first finds the first qualifying element and then deletes it from the container.