Extension to the STL "find_if" and "for_each"

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.



Comments

  • mayyouhelpme

    Posted by Legacy on 11/03/2002 12:00am

    Originally posted by: tangsilai

    hello
    I 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!!

    Reply
  • Even plainer STL

    Posted by Legacy on 08/13/1999 12:00am

    Originally posted by: Dana P'Simer

    The following code shows how to use just STL to implement
    
    the exact same functionality without the use of custom
    implementations of find_if and for_each, custom function
    objects, or custom adapter objects. It uses std::bind1st
    and std::mem_fun1 to call the memeber functions of DATA
    with the OBJECT as an argument.

    -----------------------
    #include <list>
    #include <algorithm>
    #include <iostream>

    typedef int OBJECT;

    std::list<OBJECT> theList;

    class DATA
    {
    public:
    bool IsValid(OBJECT rObject) { return rObject == 5; }
    int OperateOn(OBJECT rObject) { std::cout << rObject << std::endl; return 0; }
    };

    int main(int argc, char* argv[])
    {
    DATA theData;

    // fill the list
    for( OBJECT i = 0; i < 10; i++ )
    theList.push_back(i);

    // find the 5
    std::cout << "The results of find_if:" << std::endl;
    OBJECT theObject = *std::find_if(theList.begin(), theList.end(), std::bind1st(std::mem_fun1(DATA::IsValid),&theData) );
    std::cout << theObject << std::endl;

    // print the list
    std::cout << "The results of for_each:" << std::endl;
    std::for_each(theList.begin(), theList.end(), std::bind1st(std::mem_fun1(DATA::OperateOn),&theData));

    char c;
    std::cin >> c;
    return 0;
    }

    Reply
  • Plain STL

    Posted by Legacy on 10/27/1998 12:00am

    Originally posted by: Ithier de Lestrange

    You do not need to rewrite the find_if and for_each algorithms to have more arguments. You just need to use a
    struct (and the operator() function) as shown below:
    
    


    struct ex : public std::unary_function<OBJECT&, void>
    {
    ex (short Param1, bool Param2) :
    n_Param1(Param1), b_Param2 (Param2)
    {
    }
    void operator() (OBJECT& x)
    {
    x.Function1 (m_Param1, b_param2);
    }

    int n_Param1;
    bool b_Param2;
    };

    std::list<OBJECT> TheList;

    std::for_each (TheList.begin(), TheList.end(), ex(3, false));


    At http://www.sgi.com/Technology/STL/ you can find a very good documentation about STL.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds