The C++ STL function object, functor code sample


Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: A function object, or functor, is any type that implements operator(). This operator is referred to as the call operator or sometimes the application operator. The Standard Template Library uses function objects primarily as sorting criteria for containers and in algorithms.

Function objects provide two main advantages over a straight function call. The first is that a function object can contain state. The second is that a function object is a type and therefore can be used as a template parameter.

To do: Using C++ STL functor (function object) to return a true/false value in C++ programming

To show: How to use the C++ STL function object (functor) in C++ programming


// C++ STL function object, functor

#include <iostream>

using namespace std;


class Functor



int operator()(int a, int b)


// true, 1 if a < b, false otherwise

return a < b;




int main(void)


Functor f;

int a = 5;

int b = 7;

int ans = f(a, b);


cout<<"a is "<<a<<", "<<"b is "<<b<<" if a < b, then true or 1"<<endl;

cout<<"The return value is "<<ans<<endl<<endl;

// re-assign

a = 10;

b = 2;

ans = f(a, b);


cout<<"a is "<<a<<", "<<"b is "<<b<<" if a < b, then true or 1"<<endl;

cout<<"The return value is "<<ans<<endl;

return 0;



Output example:


a is 5, b is 7 if a < b, then true or 1

The return value is 1

a is 10, b is 2 if a < b, then true or 1

The return value is 0

Press any key to continue . . .



The last line of the main function shows how you call the function object. This call looks like a call to a function, but it is actually calling operator() of the Functor type. This similarity between calling a function object and a function is how the term function object came about. The Standard Template Library contains several function objects in the <functional> header file. One use of these function objects is as a sorting criterion for containers. For example, the set container is declared as follows:


template <

class Key,

class Traits=less<Key>,

class Allocator=allocator<Key> >

class set


The second template argument is the function object less. This function object returns true if the first parameter passed to it is less than the second parameter passed. Since some containers sort their elements, the container needs a way of comparing two elements, and this is accomplished using the function object. You can define your own sorting criteria for containers by creating a function object and specifying it in the template list for the container. Another use of functional objects is in algorithms. For example, the remove_if algorithm is declared as follows:



ForwardIterator remove_if(

ForwardIterator _First,

ForwardIterator _Last,

Predicate _Pred



The last argument to remove_if is a function object that returns a boolean value (a predicate). If the result of the function object is true, then the element is removed from the container being accessed by the iterators _First and _Last. You can use any of the function objects declared in the <functional> header for the argument _Pred or you can create your own.



C and C++ Programming Resources | C & C++ Code Example Index