谓词 仿函数 函数指针

谓词

  • 谓词(函数)是一种接受参数并且返回bool值的函数。主要是用来做一些条件检测,实际上就是起到一个判断式的作用。在C++的STL算法库<algorithm>中经常用到,例如常见的std::sort()中。根据谓词函数接受参数的个数进行分类:

    1. 一元谓词(Unary Predicate):只接收一个参数并返回bool值,如count_if (InputIterator first, InputIterator last, UnaryPredicate pred)pred就是一个一元谓词。

    2. 二元谓词( Binary Predicate):接收两个参数并返回bool值,如ForwardIterator unique (ForwardIterator first, ForwardIterator last, BinaryPredicate pred);中pred就是一个二元谓词。

  • 谓词可以是谓词函数、函数指针以及函数对象,但是其参数不能被修改。

谓词函数

  • 一元谓词:

        bool GT6(const string &s)  // 函数参数必须是const型,防止参数被修改
        {
            return s,size() >= 6;
        }
  • 二元谓词:

        bool isShorter(const string &s1, const string &s2) // 函数参数必须是const型,防止参数被修改
        {
            return s1.size() < s2.size();
        }

函数指针( function pointer)

  • 函数指针作为谓词函数,在实质上函数名就是一个函数指针,因此可以直接定义函数,然后在谓词的位置使用函数名,只不过是这个函数指针接受的参数和返回类型需要满足谓词条件。实际上谓词在使用的时候也就是使用谓词函数的函数名。所以个人猜测,实际上谓词就是一个bool (*pf)(T&)或者bool (*pf)(T1&,T2&)的函数指针。如下就是定义了一个函数(返回类型和接受参数个数必须符合具体使用的函数),直接使用函数名(函数指针)。

        void output1 (const string &s)
        {
            cout << s << ' ';
        }
        ……
        for_each(artile.begin(), artile.end(), output1);

函数对象(function object)

  • 函数对象:是特指重载了操作符operator()的类或者是结构体的实例(结构体和类有一定的相似性)。这样的实例叫做函数对象或者仿函数(functor)。下面是函数对象的实现:

        struct Mystruct 
        {
            void operator () (const string &s) {cout << s << ' ';}
        } output2;
    
        class Myclass
        {
        public:
            void operator() (const string &s) const
            {
                cout << s << ' ';
            }
        };
  • 上面的代码分别用结构体和类的方式重载了括号操作符,在调用的时候如下:

    
        /* 使用结构体申明的类 */
        for_each(artile.begin(), artile.end(), Mystruct()); //直接用结构体名加括号
        //使用申明的实例(函数对象)output2,不用加括号,使用类似函数指针
        for_each(artile.begin(), artile.end(), output2);
    
        /* 使用class申明的类 */
        for_each(artile.begin(), artile.end(), Myclass()); //直接类名加括号
        Myclass output3; //申明对象
        for_each(artile.begin(), artile.end(), output3); //直接使用对象名

头文件<functional>

  • 头文件<functional>是一个包含了很多函数对象(function object)的头文件,这些函数对象通常用来做标准算法里面的谓词(predicate)或者是比较函数(comparison function)。一般有分别和>, >=, <, <=, &, !, |, ^, &&, ||, ==, !=, /, %, +, -, *相对应的类,使用的前提是相应的数据类型定义了对应的操作。 其中less(类模板)的定义如下:
    template <class T> struct less {
      bool operator() (const T& x, const T& y) const {return x<y;}
      typedef T first_argument_type;
      typedef T second_argument_type;
      typedef bool result_type;
    };
  • 其中T是一个类型,在使用时候自己定义参数的类型。在使用sort的时候如下:

        sort(article.begin(), article.end(), less<string> ()) // 按照字符串从小到大排序
        sort(article.begin(), article.end(), greater<string> ()) // 按照字符串从大到小排序  

优缺点

To Be Cobtinued……

代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;

const string delet = "\",.:'?!" ;

/*函数参数必须是const型,防止参数被修改 */
bool isShorter(const string &s1, const string &s2) 
{
    return s1.size() < s2.size();
}

/* 函数参数必须是const型,防止参数被修改 */
bool GT6(const string &s)  
{
    return s.size() >= 6;
}


void output1 (const string &s)
{
    cout << s << ' ';
}

struct Mystruct 
{
    void operator () (const string &s) {cout << s << ' ';}
} output2;

class Myclass
{
public:
    void operator() (const string &s) const
    {
        cout << s << ' ';
    }
};


int main()
{
    Myclass output3;
    string word;
    vector<string> artile;
    fstream input("in.txt");

    while(input >> word)
    {
        /* 去除标点符号 */
        size_t pos = 0;
        while((pos = word.find_first_of(delet, pos)) != string::npos)
            word.erase(pos,1);

        artile.push_back(word); //压入容器
    }

    sort(artile.begin(), artile.end()); //按字典排序
    /* 对word重新排序 */
    vector<string>::iterator end_unique = unique(artile.begin(), artile.end());
    artile.erase(end_unique, artile.end()); //删除重复单词

    cout << endl << " Predicate Function " << endl;
    stable_sort(artile.begin(), artile.end(), isShorter);

    cout << endl << "Function Pointer: output1" << endl;
    for_each(artile.begin(), artile.end(), output1);

    cout << endl << "Function Object: Mystruct()" << endl;
    for_each(artile.begin(), artile.end(), Mystruct());

    cout << endl << "Function Object: output2" << endl;
    for_each(artile.begin(), artile.end(), output2);

    cout << endl << "Function Object: Mclass()" << endl;
    for_each(artile.begin(), artile.end(), Myclass());

    cout << endl << "Function Object: output3" << endl;
    for_each(artile.begin(), artile.end(), output3);


    vector<string>::size_type wc = count_if(artile.begin(), artile.end(), GT6);
    cout << endl << wc <<" " << "6 characters or longer." << endl;
    return 0;
}

相关参考文章:
C++ - 算法(algorithm) 的 谓词(predicate) 详解
浅析STL 谓词 + 仿函数 + 函数指针(c)
c++谓词函数和仿函数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值