Extension to the STL "find_if" and "for_each"
Posted
by Michael Scherotter
on August 6th, 1998
To use:
std::list<OBJECT> theList;
class DATA
{
public:
bool IsValid(const OBJECT& rObject) const;
void OperateOn(OBJECT& rObject) const;
};
bool IsValid(OBJECT theObject, DATA theData)
{
return theData.IsValid(theObject);
}
void DoOperation(OBJECT theObject, DATA theData)
{
theData.OperateOn(theObject);
}
DATA theData;
OBJECT theObject = ::TDFind_if(theList.begin(), theList.end(), IsValid, theData);
or
::TDfor_each(theList.begin(), theList.end(), DoOperation, theData);
// CODE STARTS HERE:
/////////////////////////////////////////////////////////////////
// This is like the Standard c++ algorithm find_if, but it takes a second parameter
template<class _II, class _Pr, class T_DATA> inline
_II
TDfind_if(
_II _F, // First iterator
_II _L, // Last iterator
_Pr _P, // Predicate: a static function that returns bool having two
paramters
T_DATA dwData)
{
for (; _F != _L; ++_F)
{
if (_P(*_F, dwData))
{
break;
}
}
return (_F);
}
template<class _II, class _Fn, class T_DATA> inline
void
TDfor_each(
_II _F,
_II _L,
_Fn _Op,
T_DATA dData)
{
for (; _F != _L; ++_F)
{
_Op(*_F, dData);
}
}
Date Posted: 5/4/98
Posted by: Pat Laplante.

Comments
mayyouhelpme
Posted by Legacy on 11/03/2002 12:00amOriginally posted by: tangsilai
hello
ReplyI am a new man in this aspect, And I want some comments of your how to use c++ freely.I am too sad to found it is to hard to learn.Help me please!!
Even plainer STL
Posted by Legacy on 08/13/1999 12:00amOriginally posted by: Dana P'Simer
ReplyPlain STL
Posted by Legacy on 10/27/1998 12:00amOriginally posted by: Ithier de Lestrange
Reply