The stl algorithms for_each and find_if are inadequate when
you need more data for the predicate. Here are two new algorithms
TDfind_if and TDfor_each which solve that problem.
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.