Passing functors to standard algorithms I created 3 different implementations (which produce the same results) using the count_if function from STL. I wish to check the number of elements in the vector which are strictly less than 6. 

These are:


C++ lambda function 
Global function (no functor)
Using class and creating my own functor


These implementations work, but, how can I improve them into a more efficient and robust solutions?

#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;numeric&gt;
#include &lt;functional&gt;
#include &lt;iostream&gt;

using namespace std;

// GLOBAL function (not FUNCTOR) See below for FUNCTOR implementation! 
template &lt;int num&gt;
bool lessThan(int x)
{
    return(x &lt; num); // return TRUE if the value is less than NUM
}

// Paramterized Function with more robust FUNCTOR implementation (written by me) (I know stl has this built in)
class LessThan 
{
private:
    int m_x; // the value we wish to be less than 
public:
    LessThan(int val) : m_x(val) {} // custom constructor: assign a value to x
    bool operator() (int upper) // all values must be strictly less than the upper bound 
    {
        return (m_x &lt; upper);
    }
};

int main()
{
    vector&lt;int&gt; vec = { 2, 7, 4, 5, 7, 7};

    // Using new C++ Lambda function implementation
    int x = count_if(vec.begin(), vec.end(), [](int x) {return x &lt; 6; }); // using C++ Lambda Function (just a function with no name)
    cout &lt;&lt;"There exists " &lt;&lt; x &lt;&lt; " integers in vec which are strictly less than 6." &lt;&lt; endl;

    // Using global lessThan6 implementation (NOT FUNCTOR)
    int y = count_if(vec.begin(), vec.end(), lessThan&lt;6&gt;); // not very robust 
    cout &lt;&lt; "There exists " &lt;&lt; y &lt;&lt; " integers in vec which are strictly less than 6." &lt;&lt; endl;

    // Using MY OWN FUNCTOR implementation 
    int z = count_if(vec.begin(), vec.end(), LessThan(6)); // robust 
    cout &lt;&lt; "There exists " &lt;&lt; z &lt;&lt; " integers in vec which are strictly less than 6." &lt;&lt; endl;

}
I'm not sure entirely what you're asking. If you're asking for which of three is "better" code between:

int x = count_if(vec.begin(), vec.end(), [](int x) {return x &lt; 6; });
int y = count_if(vec.begin(), vec.end(), lessThan&lt;6&gt;);
int z = count_if(vec.begin(), vec.end(), LessThan(6));


I would say x then z then y. The reason for this is that the lambda solution is the only one where the actual functor is in-line with the algorithm. Anybody reading that code knows exactly what x should be immediately. There is no question. It is clear (to the extent that lambda syntax is) and concise. 

z would be the C++03 way of doing this thing. It's... ok. We wrote that code for many years. The problem is the body of the functor is often hundreds of lines removed from the call, maybe in a different file... so readers of your code will have to find it. And it's never going to be perfectly clear either (who knows, maybe you named your functor weird and it's returning 6 &lt; x? Naming things is really hard)

y is the worst. Having 6 as a template argument adds nothing to your performance (it's not like you're optimizing out a comparison), has equal readability loss compared to the lambda with the functor, and on top of that, can't even be called with a variable. With the other two, I can easily write:

int threshold = get_threshold();
int x = count_if(vec.begin(), vec.end(), [=](int x) {return x &lt; threshold; });
int z = count_if(vec.begin(), vec.end(), LessThan(threshold));


But there's no way to pass threshold in as a template.

Typically, for algorithms, strongly prefer lambda unless (1) the algorithm call is a small part of the function and (2) the functor is very long and complicated. I say both must be met because something like this is still perfectly acceptable in my opinion:

int countBySomeCriteria() {
    return count_if(vec.begin(), vec.end(), [=](Foo const&amp;) {
        /* some 15 line complicated functor */
    });
}


whereas if that call were surrounded by a lot of other logic, it would be much harder to easily make out. 

Lambdas got added to the language basically to make stuff like this easier to write and easier to read. You should use them because they succeed in that goal. 



PS. There is a fourth way to pass a functor: the lambda that was a lambda before C++11 lambdas: Boost.Lambda:

int q = count_if(vec.begin(), vec.end(), _1 &lt; 6);


Make of that what you will.