开发者

Why is this program overloading () operator?

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;
        lo开发者_C百科ng 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.


The class's purpose is to implement a function that sorts the elements in the set in a given way. This is known as a predicate.

It is implemented as a functor, i.e. by allowing the function operator to be used on an object (this is what std::set does beneath the hood). Doing so is the common way for STL and similar code to call custom objects. (A function pointer is more limited than a function object (a.k.a. functor)

So it's used like this:

Unitless functor;

functor(123, 124); // returns true or false

std::set is a sorted binary tree, so it calls the Unitless's ()-operator several times on each insert to determine where each long value should go.

Try compiling and putting some printf/std::cout in there and see what happens.

Also, be aware that callbacks like this (i.e. when you can't see the call to your code) are scary and confusing in the beginning of your learning curve.

  • Then you get used to it and use them all over, because they are neat.
  • Then your code gets scary and confusing again and you avoid them like the plague.
  • Then you become a duct-tape programmer and use them only where appropriate, but never elsewhere.

;)


UnitLess is a binary predicate, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜