User Defined Predicates

Just some simple examples posted here as a means of easy lookup...
class MyValue
{
      public:
            MyValue::MyValue( int v ) : value( v ) {}
      int value;
};

class MyValue_eq : public unary_function< MyValue, bool >
{
      int value;
public:
      explicit MyValue_eq( const int& val ) : value( val ) {}

      bool operator() ( const MyValue& c ) const
      {
            return c.value == value;
      }
};

vector< MyValue > mvals;
MyValue m1( 1 );
MyValue m2( 2 );
MyValue m3( 3 );
MyValue m4( 4 );

mvals.push_back( m1 );
mvals.push_back( m2 );
mvals.push_back( m3 );
mvals.push_back( m4 );

typedef vector< MyValue >::iterator mit;

mit p_mit = find_if( mvals.begin(),
                     mvals.end(),
                     MyValue_eq( 2 ) );

int x = (*p_mit).value;

Comments

Popular posts from this blog

Using the Supervisor Controller Pattern to access View controls in MVVM

Getting started with Tesseract optical character recognition (OCR) library in Visual Studio

How to send an e-mail via Google SMTP using C#