When I've start seriously using STL algorithms, I suddenly realized how much I miss a way to define anonymous class or functions. For example, it is a quite common need to run an STL algorithm based on some of the prooperty of object a container is holding. In this case, you should have defined an unarary functor for each of the ordering methods and, of couse, every time you need another, another functor shall be written.

Lambda functions have beed introduced in C++11 and i find that a conveniente and expressive solution but still you could not use C++11 (as I do) or you may want to not repeat yourself each time you call an STL algorithm to specify the functor to use as a lambda.

I've written some article using ordering as my use case but, as the helper infrastructure I'm going to describe apply in many different cases, this time I'll use a search use case.

Consider a situation with a container filled with a number of objects of type A defined as:

class A { int i ; string s ; public : A ( int i , string s ){ this -> i = i ; this -> s = s ; } string getName () const { return s ; } void setName ( string s ){ this -> s = s ; } int getID () const { return i ; } void setID ( int n ){ i = n ; } }; int main ( int argc , char * argv []){ vector < A > v ; A a ( 2 , "foo" ); A b ( 7 , "bar" ); A c ( 2 , "foo" ); v . push_back ( a ); v . push_back ( b ); v . push_back ( c ); ...

What if you want to find the first element in v whose name is foo ?

My naive default approch My naive approch are actually two depending of the generality of the equality criteria I'm about to implement. If the criteria is common enough, I can expand the A class with an inner class defining the equality criteria such as: class A { ... public : struct EqualName { EqualName ( string target ){ t = target ; } bool operator () ( A & a ){ return a . getName () == t ; } private : string t ; }; }; int main ( int argc , char * argv []){ ... vector < A >:: const_iterator x = find_if ( v . begin (), v . end (), A :: EqualName ( "foo" ) ); if ( x == v . end ()) cout << "not found" << endl ; ... } If the criteria is less common and think I'll not use it in other places, I write the struct locally, for example in the main function. To be honest I dislike this solutions, thinking they are good only to manage much more particular property matching. So I've spent some time to find a better solution to better use the fact that my match is simply based on an object property.