Fork me on GitHub

C++ like 'string' transform ...

written on April 05, 2009

All we need is a naked class with the () operator overloaded to execute and wrap the "real" tolower.

struct toLower
{
    int operator()(int ch)
    {
        return tolower(ch);
    };
};

With this our transform will resume to this beautiful and elegant line ...

transform( s.begin(), s.end(), s.begin(), toLower() );

... instead of the very 'C like' looking (even if we used C++ like casting):

transform( s.begin(), s.end(), s.begin(), (int(*)(int))tolower );

Pretty, pretty :D