views:

129

answers:

3

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.

+7  A: 

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);
Space_C0wb0y
Sorry I beginner and I can't understand what you are trying to say : (
Searock
@Space_C0wb0y operator () is being called internally when it executes store.insert(some long value);
Searock
Why do you need to provide a predicate object? IIRC, `std::set<T, Pred>` will use a default-constructed predicate (I.e. `Pred()`). Here, `UnitLess` is default-constructable. Besides, if you didn't provide a predicate _object_, what would `std::set<T, Pred>` do otherwise? It obviously can't use `std::less<T>()`
MSalters
@MSalters: I was just thinking about that too, actually was just about to test it, because I could find no documentation that says that the object would be default constructed.
Space_C0wb0y
@MSalters If I don't pass Unitless then it sorts them in ascending order. But I want to sort them according to their unit's place. For example 150 and 145. In this example 0 is less than 5 so I will sort in a custom oder 150 then 145.
Searock
+1 Thanks a lot.
Searock
@Searock: my comment referred to an older version of this answer, which stated that you needed to pass both the `Unitless` type as a template parameter and an `Unitless` object to the constructor. The latter is redundant in this case.
MSalters
+7  A: 

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.

;)

Marcus Lindblom
+1 Thanks a lot.
Searock
+1  A: 

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.

CashCow
@CashCow Are you sure it as not as `Associative Container`? http://www.sgi.com/tech/stl/set.html says that it is `Sorted Associative Container`.
Searock
The documentor of sgi.com thinks it is an associative container but that does not seem to match the description at wikipedia i.e. http://en.wikipedia.org/wiki/Associative_array where it appears there should be some value associated with the key. You could say that a key maps a value to itself. Of course the implementors of std::set and the equivalent in any other language may well wish to use the same implementation they use for map, i.e. a tree.
CashCow