谓词 仿函数 函数指针
来源:互联网 发布:varier 知乎 编辑:程序博客网 时间:2024/06/14 10:38
谓词
谓词(函数)是一种接受参数并且返回bool值的函数。主要是用来做一些条件检测,实际上就是起到一个判断式的作用。在C++的STL算法库
<algorithm>
中经常用到,例如常见的std::sort()
中。根据谓词函数接受参数的个数进行分类:一元谓词(Unary Predicate):只接收一个参数并返回bool值,如
count_if (InputIterator first, InputIterator last, UnaryPredicate pred)
中pred
就是一个一元谓词。二元谓词( 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++谓词函数和仿函数
- 谓词 仿函数 函数指针
- 浅析STL 谓词 + 仿函数 + 函数指针(c)
- 谓词函数predicates和仿函数functors
- 谓词函数
- 仿函数 括号重载 谓词函数比较类数据成员
- 函数指针与仿函数
- 仿函数与函数指针
- 谓词函数、函数对象
- C++: 谓词函数、函数对象(仿函数)、回调函数总结
- 指定谓词函数排序
- 函数对象 谓词
- C++中的谓词函数
- 函数对象和谓词
- C++函数对象-谓词
- Guava谓词函数
- 函数指针、函数对象、仿函数比较与入门
- 仿函数 <- 函数指针 成员函数 std::bind
- 函数指针、函数对象、仿函数比较与入门
- eclipse for javaEE
- Machine Learning Python Code --- Gaussian Mixture Model
- spark streaming读取kafka 零丢失(三)
- slf4j 和 log4j合用的(Maven)配置
- TCP/IP为什么需要三次握手链接及四次握手关闭(个人理解)
- 谓词 仿函数 函数指针
- Android之intent传值的三种方法
- Netty4.0学习笔记系列之一:Server与Client的通讯
- 11. Container With Most Water
- java开发RtspClient
- linux 查看文件夹下文件大小命令
- Struts2的验证机制
- Java 同步 synchronized与lock
- zoj 3747 Attack on Titans【递推好题】