函数对象

寵の児 提交于 2019-12-01 09:49:33
数组
enum eRelation                                  // 表示计算策略的枚举体
{
    eLess,                                      // 小于
    eEqual,                                     // 等于
    eGreater                                    // 大于
};
template< typename T, typename ITER_TYPE>
ITER_TYPE find_if_number( ITER_TYPE begin, ITER_TYPE end,
                  eRelation relType, const T &target )
                                                // 用数值表示策略的find_if函数
{
    for(ITER_TYPE iter = begin; iter!=end; iter++) // 遍历迭代器区间
    {
        switch( relType )                           // 根据不同的策略
        {
        case eLess:
            if( *iter < target )                    // 比较当前元素与目标
            {
                return iter;                        // 返回迭代器
            }
        case eEqual:
          if( *iter == target )
            {
                return iter;
            }
        case eGreater:
          if( *iter > target )
            {
                return iter;
            }
        }
    }
    return end;                                     // 失败,则返回区间尾部
}
int main(int argc, char *argv[])                    // 主函数
{
    int ary[5] = {1,2,3,4,5};                       // 数组
    vector vec(ary, ary+5);                    // 构造向量
    vector::iterator iter;                     // 定义迭代器
    iter = find_if_number( vec.begin(), vec.end(), eEqual, 3);  // 应用数值策略查找
    cout<< *iter <<endl;                            // 输出查找结果3
    return EXIT_SUCCESS;
 
 
指针
 
 
bool Equal(const int &ele, const int &target)       // 策略实现函数
{
    return ele == target;
}
template< typename T, typename ITER_TYPE >          // 模板参数
ITER_TYPE find_if_fun_ptr( ITER_TYPE begin, ITER_TYPE end,// 迭代器区间参数
        bool (*PFUN)(const T &, const T &),         // 表示算法策略的函数指针
        const T& target )                           // 查找目标
{
    for(ITER_TYPE iter = begin; iter!=end; iter++) // 遍历迭代器区间
    {
      if(PFUN(*iter, target))                       // 应用算法策略(函数)
      {
            return iter;                            // 返回迭代器
        }
    }
    return end;                                     // 失败,则返回区间尾部
}
int main(int argc, char *argv[])// 主函数
{
    int ary[5] = {1,2,3,4,5};                       // 定义数组
    vector vec(ary, ary+5);                    // 定义向量
    vector::iterator iter;                     // 定义迭代器
    iter = find_if_fun_ptr(vec.begin(), vec.end(), Equal, 3);
                                                    // 使用函数指针表达算法策略
    cout<< *iter <<endl;                            // 输出结果3
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
 函数对象
template
struct equal2 // 函数对象
{
    bool operator()(const int &ele, const int &target)// 重载函数调用运算符
    {
        return ele == target;
    }
};
template< typename T, typename ITER_TYPE, typename Comp >
ITER_TYPE find_if_fun_obj( ITER_TYPE begin, ITER_TYPE end,// 迭代器区间
        Comp funObj,                                   // 函数对象参数
        const T& target )                              // 目标
{
    for(ITER_TYPE iter = begin; iter!=end; iter++)     // 遍历迭代器区间
    {
    if(funObj(*iter, target))                          // 应用算法策略
    {
          return iter;
        }
    }
    return end;
}
int main(int argc, char *argv[])                       // 主函数
{
    int ary[5] = {1,2,3,4,5};                          // 定义数组
    vector vec(ary, ary+5);                       // 定义向量
    vector::iterator iter;                        // 定义迭代器
    iter = find_if_fun_obj(vec.begin(), vec.end(), equal2(), 3);
                                                       // 使用函数对象,查找目标
    cout<< *iter <<endl;                               // 输出结果3
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!