`
bcyy
  • 浏览: 1832535 次
文章分类
社区版块
存档分类
最新评论

C++愤恨者札记7——函数适配器

 
阅读更多
C++愤恨者札记7——函数适配器

考虑编写一个通用的计数函数TestCount,再用它来统计字符串空格的个数:
#include <string>
#include <iostream>
using namespace std;

bool TestIsspace( wchar_t ch )
{
    if ( ch == ' ' )
        return true;
    else
        return false;
}

template <class T, class Fn >
int TestCount( T p1, T p2, Fn pFn )
{
    int ret = 0;
    for (; p1 != p2; p1 ++ )
        if ( pFn(*p1) )
            ret++;

    return ret;
}

void main()
{
    wstring s = L"  hel  lo   ";
    wchar_t buf[] = L"  hel  lo   ";

    cout << TestCount(s.begin(), s.end(), TestIsspace ) << endl;
    cout << TestCount(buf, buf+wcslen(buf), TestIsspace ) << endl;
}


再考虑用TestCount统计非空格字符的数量。你可能会想,再写一个TestIsspaceNeg,里面调用TestIsspace把结果取反用于返回。
bool TestIsspaceNeg( wchar_t ch )
{
    return !TestIsspace( ch );
}
把TestIsspaceNeg作为TestCount的第三个参数传入不就完了吗?虽然这个方法很有效,但未免有点烦琐。类似于TestIsspace的函数多了去了,而且还得经常用到,比如库函数isspace, isalpha, isdigit等,是不是每次要取补集时都要“重写”,这些函数呀?有没有通用的方法,可以把各个函数的返回值取反再返回?函数指针!这样就可以管一块拥有相同函数签名的函数了。看看行不行。
bool TestGenericNeg( bool (*pFn)(wchar_t), wchar_t ch )
{
    return !pFn(ch);
}
要生成TestIsspace的反函数,直接把函数指针扔给TestGenericNeg就行了。
TestGenericNeg( TestIsspace, ch);

嗯,这是个进步,不管是TestIsspace还是库函数都可以用,只要函数签名是bool ()(wchar_t)都可以用。但有没有发现这个TestGenericNeg参数不只一个了,而且两个参数缺一不可,一个是原函数,另一个是原函数的参数。只把TestGenericNeg给TestCount显然不行,你必须为TestGenericNeg指定使用的函数。仅仅使用TestGenericNeg的函数指针是不行的,它的信息太有限了。这也是函数指针的致命缺陷。而解决这个缺陷的方法是使用函数对象(Function Object),就是使用重载了调用操作符(就是括号)的类。类对象可以携带更多的信息,包括原函数指针。
#include <string>
#include <iostream>
using namespace std;

bool TestIsspace( wchar_t ch )
{
    if ( ch == ' ' )
        return true;
    else
        return false;
}

template <class T, class Fn >
int TestCount( T p1, T p2, Fn pFn )
{
    int ret = 0;
    for (; p1 != p2; p1 ++ )
        if ( pFn(*p1) )
            ret++;

    return ret;
}

//为给其它函数使用,这里用模板
template <class Fn, class T>
class NegClxEnclose
{
    public:
        Fn pFn;
        NegClxEnclose( Fn pFn )
        {
            this->pFn = pFn;
        }
        bool operator () ( T ch )
        {
            return !this->pFn( ch );
        }
};

void main()
{
    
    wstring s = L"  hel  lo   ";

    NegClxEnclose<bool (*)(wchar_t), wchar_t> negObj(TestIsspace);

    cout << TestCount(s.begin(), s.end(), negObj ) << endl;
    cout << TestCount(s.begin(), s.end(), NegClxEnclose<bool (*)(wchar_t), wchar_t>(TestIsspace) ) << endl;    //为方便一些,使用临时对象
}
这时,已经达到了我们的目标,NegClxEnclose是一个通用的,合格的取反器了。但发现在使用它的时候,都得手动填充数据类型,像上面的<bool (*)(wchar_t), wchar_t>,实在太麻烦,有点不完美。有什么办法可以让编译器帮我们填充呢?答案是:有,函数模板。在我们调用函数模板时,就像调用普通函数一样,从来不要手动填充数据类型,这份工作由编译器代劳了。于是我们可以利用这一点。
最终的代码可以写成:
#include <string>
#include <iostream>
using namespace std;

bool TestIsspace( wchar_t ch )
{
    if ( ch == ' ' )
        return true;
    else
        return false;
}

template <class T, class Fn >
int TestCount( T p1, T p2, Fn pFn )
{
    int ret = 0;
    for (; p1 != p2; p1 ++ )
        if ( pFn(*p1) )
            ret++;

    return ret;
}

template <class Fn, class T>
class NegClxEnclose
{
    public:
        Fn pFn;
        NegClxEnclose( Fn pFn )
        {
            this->pFn = pFn;
        }
        bool operator () ( T ch )
        {
            return !this->pFn( ch );
        }
};

template <class RetT, class T >
NegClxEnclose<RetT (*)( T ), T> ImproveFn( RetT (*pFn)( T )  )
{
    return NegClxEnclose<RetT (*)( T ), T>(pFn);
}

void main()
{
    wstring s = L"  hel  lo   ";
    cout << TestCount(s.begin(), s.end(), ImproveFn( TestIsspace ) ) << endl;    //是不是很简洁
}

是不是很高明? 其实C++标准库里面早就有这些工具了!这就是函数适配器Adaptor。下面是使用标准库实现的代码:
#include <string>
#include <iostream>
#include <functional> //for not1
using namespace std;

bool TestIsspace( wchar_t ch )
{
    if ( ch == ' ' )
        return true;
    else
        return false;
}

template <class T, class Fn >
int TestCount( T p1, T p2, Fn pFn )
{
    int ret = 0;
    for (; p1 != p2; p1 ++ )
        if ( pFn(*p1) )
            ret++;

    return ret;
}

void main()
{
    
    wstring s = L"  hel  lo   ";
    cout << TestCount(s.begin(), s.end(), not1( ptr_fun(TestIsspace)) ) << endl;
}
所不同的是,not1接收的是一个函数对象而不是函数指针,所以要用ptr_fun对TestIsspace进行转换。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics