Question: Currently I am studying Standard Template Library (STL).

In this program I am storing some long values in Associative Container and then sorting them according to unit's place (according to the number in unit's place).

**Code :** 

```
#include <iostream>
#include <set>
#include <functional>

using namespace std;

class UnitLess
{
public:
    bool operator()(long first, long second)
    {
        long fu = first % 10;
        long su = second % 10;

        return fu < su;
    }
};

typedef set<long, UnitLess> Ctnr;

int main(void)
{
    Ctnr store;

    store.insert(3124);
    store.insert(5236);
    store.insert(2347);
    store.insert(6415);
    store.insert(4548);
    store.insert(6415);

    for(Ctnr::iterator i = store.begin(); i != store.end(); ++i)
    {
        cout << *i << endl;
    }
}

```

But I don't understand why our Professor has overloaded () operator ?

Thanks.

Answer: 
`UnitLess` is a [binary predicate](http://www.sgi.com/tech/stl/BinaryPredicate.html), that is required to be a callable with two parameters in STL.

By overloading the function-call operator in `UnitLess` instances of that type can be used like a function that takes two `longs` and returns a `bool`.

```
UnitLess f;
f(5, 10);

```

is equivalent to 

```
bool f(long first, long second)
{
    long fu = first % 10;
    long su = second % 10;

    return fu < su;
}
f(5, 10);

```
He has implemented a custom comparison used for std::set that only compares the units i.e. the amount modulo 10. Because std::set is a template, it just tries to call something that looks like a function, whether or not it is one or not. By overloading operator() you make it act like a function.

Doing this can actually in some circumstances be extremely powerful because a struct / class can store state as well as extra parameters. The whole of boost::function / boost::bind is based on doing this (and you don't need to create a class each time).

In the actual example coded there is perhaps a slight flaw as the exercise was simply to sort them by units but it could well eliminate numbers that have the same units but are not actually duplicates. In your sample code there are no such examples (you have a duplicate but that is a duplicate of the whole value). If you had 3478 in addition to 4548, the Set comparator would consider them the same and would not permit the duplicate.

By the way, I am not sure set is what I would call an "associative" container, which refers to key-value pairs. There are no associated values in set, just keys.

One more point: operator() should be const.