C++ STL 学习 :更多仿函数(functor)(二)

12085阅读 0评论2010-07-21 zieckey
分类:C/C++

By zieckey( All right reserved!)

C++ STL 学习 :更多仿函数(functor)(二)

C++标准程序库中提供了许多非常有用的预先定义好的的仿函数,了解这些将为我们的开发工作带来便利性和稳健性。

求反值:

// TEMPLATE STRUCT negate

template<class _Ty>
    struct negate
        : public unary_function<_Ty, _Ty>
    {
// functor for unary operator-

    _Ty operator()(const _Ty& _Left) const
        {
// apply operator- to operand

        return (-_Left);
        }
    };


例如我们对一个vector中所有元素求反:

transform( vect.begin(), vect.end(), vect.begin(), std::negate() ); 

   
transform参数1:第一个集合的开始(包含)
transform参数2:第一个集合的结束(不包含)
transform参数3:第二个集合的开始(包含)
transform参数4:对第一个集合中的每一个元素应用这个仿函数,仿函数的返回值放到第二个集合中
经过上面解释,那么 transform 算法就是使用 negate 仿函数,将第一个集合中的所有元素求反值之后转移到第二个集合中。这里第二个集合就是自己,那么这段程序就是对“集合中每个元素求反值”


下面看一些更为复杂的仿函数。下面一条语句操作结果是:将容器中所有小于5的元素删除。

std::remove_if( ivec.begin(), ivec.end(), std::bind2nd( std::less(), 5 ) );



有点头大了,好长,好多陌生的语法。这里我们一点点解释。
std::less是一个仿函数结构体,不用多说,直接上源码,非常好懂(其实只要理解了operator()就非常好懂这些仿函数)。

// TEMPLATE STRUCT less

template<class _Ty>
    struct less
        : public binary_function<_Ty, _Ty, bool>
    {
// functor for operator<

    bool operator()(const _Ty& _Left, const _Ty& _Right) const
        {
// apply operator< to operands

        return (_Left < _Right);
        }
    };




再看std::bind2nd是一个函数,返回一个 std::binder2nd 的仿函数结构体,两个的源码一并放上:



        // TEMPLATE FUNCTION bind2nd

template<class _Fn2,
    class _Ty> inline
    binder2nd<_Fn2> bind2nd(const _Fn2& _Func, const _Ty& _Right)
    {
// return a binder2nd functor adapter

    typename _Fn2::second_argument_type _Val(_Right);
    return (std::binder2nd<_Fn2>(_Func, _Val));
    }


        
// TEMPLATE CLASS binder2nd

template<class _Fn2>
    class binder2nd
        : public unary_function<typename _Fn2::first_argument_type,
            typename _Fn2::result_type>
    {
// functor adapter _Func(left, stored)

public:
    typedef unary_function<typename _Fn2::first_argument_type,
        typename _Fn2::result_type> _Base;
    typedef typename _Base::argument_type argument_type;
    typedef typename _Base::result_type result_type;

    binder2nd(const _Fn2& _Func,
        const typename _Fn2::second_argument_type& _Right)
        : op(_Func), value(_Right)
        {
// construct from functor and right operand

        }

    result_type operator()(const argument_type& _Left) const
        {
// apply functor to operands

        return (op(_Left, value));
        }

    result_type operator()(argument_type& _Left) const
        {
// apply functor to operands

        return (op(_Left, value));
        }

protected:
    _Fn2 op;
// the functor to apply

    typename _Fn2::second_argument_type value;
// the right operand

    };



bind2nd有两个参数:
第一个参数param1是一个仿函数(这里是std::less),该仿函数必须是带有两个参数的函数。
第二个参数param2一个普通参数,当binder2nd起作用的时候,param2会作为param1这个仿函数的第二个参数(bind2nd,如果是bind1st就是第一个参数)传给param1这个仿函数。

看binder2nd中的operator()重载函数:
result_type operator()(const argument_type& _Left) const
        {    // apply functor to operands
        return (op(_Left, value));
        }


这里的op就是bind2nd的第一个参数(是一个仿函数,这里是std::less),_Left就是这个仿函数被调用的时候由调用这传入(这里是remove_if),value就是bind2nd的第二个参数(这里是数值 5 )。
OK到这里remove_if中最后一个仿函数参数应该就比较清楚了。


下面说remove_if。说remove_if之前先说find_if。因为remove_if中调用了find_if。


        // TEMPLATE FUNCTION remove_if

template<class _FwdIt,
    class _Pr> inline
    _FwdIt remove_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
    {
// remove each satisfying _Pred

    _First = std::find_if(_First, _Last, _Pred);
    if (_First == _Last)
        return (_First);
// empty sequence, all done

    else
        {
// nonempty sequence, worth doing

        _FwdIt _First1 = _First;
        return (_STDEXT unchecked_remove_copy_if(++_First1, _Last, _First, _Pred));
        }
    }
    
template<class _InIt,
    class _Pr> inline
    _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
    {
// find first satisfying _Pred

    _ASSIGN_FROM_BASE(_First,
        _Find_if(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Pred));
    return (_First);
    }


        
// TEMPLATE FUNCTION find_if

template<class _InIt,
    class _Pr> inline
    _InIt _Find_if(_InIt _First, _InIt _Last, _Pr _Pred)
    {
// find first satisfying _Pred

    _DEBUG_RANGE(_First, _Last);
    _DEBUG_POINTER(_Pred);
    for (; _First != _Last; ++_First)
        if (_Pred(*_First))
            break;
    return (_First);
    }



从集合_First到_Last中找到第一个符合_Pred条件的值。这里的_Pred是一个仿函数,就是上面提到的std::bind2nd( std::less(), 5 )返回的那个仿函数结构体 std::binder2nd。那么很清楚,就是找到集合中第一个小与5的元素。

找到之后,就最终会调用 _Remove_copy_if,看下面代码:


        // TEMPLATE FUNCTION remove_if

template<class _FwdIt,
    class _Pr> inline
    _FwdIt remove_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
    {
// remove each satisfying _Pred

    _First = std::find_if(_First, _Last, _Pred);
    if (_First == _Last)
        return (_First);
// empty sequence, all done

    else
        {
// nonempty sequence, worth doing

        _FwdIt _First1 = _First;
        return (_STDEXT unchecked_remove_copy_if(++_First1, _Last, _First, _Pred));
        }
    }
    
    
    
    template<class _InIt,
    class _OutIt,
    class _Pr> inline
    _OutIt unchecked_remove_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred)
    {
// copy omitting each element satisfying _Pred

        return _STD _Remove_copy_if(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest, _Pred,
            _STD _Range_checked_iterator_tag());
    }
    
    
    
    
        
// TEMPLATE FUNCTION remove_copy_if

template<class _InIt,
    class _OutIt,
    class _Pr> inline
    _OutIt _Remove_copy_if(_InIt _First, _InIt _Last, _OutIt _Dest, _Pr _Pred, _Range_checked_iterator_tag)
    {
// copy omitting each element satisfying _Pred

    _DEBUG_RANGE(_First, _Last);
    _DEBUG_POINTER(_Dest);
    _DEBUG_POINTER(_Pred);
    for (; _First != _Last; ++_First)
        if (!_Pred(*_First))
            *_Dest++ = *_First;
    return (_Dest);
    }




_Remove_copy_if就很简单的一个函数了,就是每找到一个符合要求的元素就将后面所有元素前移动一位,从而实现删除这个元素的操作。

OK。至此remove_if这条语句解释清楚了。



跟remove_if类似的情况还有很多,例如:replace_if 。
看下面一条语句,就是将集合中等于





看整体程序:


#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>

struct Printer
{
    template< typename T >
    void operator()( T& _val )
    {
        std::cout << _val << ", ";
    }
};

int main()
{
    std::vector<int> ivec;
    const int MAX_COUNT = 10;
    for ( int i = 1; i < MAX_COUNT; ++i )
    {
        ivec.push_back( i );
        ivec.push_back( MAX_COUNT - i );
    }

    std::cout << "initialize : ";
    for_each( ivec.begin(), ivec.end(), Printer() );
    transform( ivec.begin(), ivec.end(), ivec.begin(), std::negate<int>() );
//将所有元素求反值

    std::cout << "\nafter negate : ";
    for_each( ivec.begin(), ivec.end(), Printer() );
    transform( ivec.begin(), ivec.end(), ivec.begin(), std::negate<int>() );
//将所有元素求反值

    std::cout << "\nafter negate twice : ";
    for_each( ivec.begin(), ivec.end(), Printer() );

    
//删除掉vector中小于5的所有元素,注意remove_if并不改变容器的大小,所以还需要调用erase来删除

    std::vector<int>::iterator iter = std::remove_if( ivec.begin(), ivec.end(), std::bind2nd( std::less<int>(), 5 ) );
    ivec.erase( iter, ivec.end() );
    std::cout << "\nafter remove_if[ < 5 ] : ";
    for_each( ivec.begin(), ivec.end(), Printer() );


    
//将集合中所有等于6的元素值改为60.

    std::replace_if( ivec.begin(), ivec.end(), std::bind2nd( std::equal_to<int>(), 6 ), 60 );
    std::cout << "\nafter replace_if[ 6 --> 60 ] : ";
    for_each( ivec.begin(), ivec.end(), Printer() );

    std::cout << std::endl;
    system( "pause" );
    return 0;
}
[/cpp]


输出:






initialize : 1, 9, 2, 8, 3, 7, 4, 6, 5, 5, 6, 4, 7, 3, 8, 2, 9, 1,
after negate : -1, -9, -2, -8, -3, -7, -4, -6, -5, -5, -6, -4, -7, -3, -8, -2, -9, -1,
after negate twice : 1, 9, 2, 8, 3, 7, 4, 6, 5, 5, 6, 4, 7, 3, 8, 2, 9, 1,
after remove_if[ < 5 ] : 9, 8, 7, 6, 5, 5, 6, 7, 8, 9,
after replace_if[ 6 --> 60 ] : 9, 8, 7, 60, 5, 5, 60, 7, 8, 9,
请按任意键继续. . .


上一篇:C++ STL 学习 :for_each与仿函数(functor)(一)
下一篇:线程间共享数据的冲突问题和 volatile 关键字