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進行轉換。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章