转自:https://blog.csdn.net/caroline_wendy/article/details/15378055
1.函数(function)谓词
通过传递函数名, 匹配二元谓词(binary predicates), 根据函数提供的策略, 输出值;
/*Function Predicate*/
bool isLarger (const std::string &s1, const std::string &s2) {
return s1.size() > s2.size();
}
......
std::stable_sort(sv.begin(), sv.end(), isLarger);
2.函数指针(function pointer)谓词
建立一个函数指针, 传入算法, 使用指针代替函数名, 用法类似函数谓词.
bool (*pf) (const std::string &s1, const std::string &s2);
pf = &isLarger;
std::stable_sort(sv.begin(), sv.end(), *pf);
3.Lambda表达式(lambda expression)谓词
Lambda表达式格式: [capture list] (parameter list) -> return type { function body }
需要匹配谓词数, 一元(unary) 或 二元(binary), 也可以通过[capture list]传递函数的变量;
std::stable_sort(sv.begin(), sv.end(),
[](const std::string& s1, const std::string& s2){ return s1.size()>s2.size(); });
4.函数对象(Function Object)谓词
类中重载函数的调用"()", 使类可以被调用, 并且传入算法谓词中, 进行使用.
/*Function Object*/
class LargerString {
public:
bool operator() (const std::string& a, const std::string& b) {
return a.size() > b.size();
}
};
......
std::stable_sort(sv.begin(), sv.end(), LargerString());
5.库定义的函数对象(Library-Defined Function Object)谓词
使用标准库定义的函数对象, 充当算法中的谓词, 包含在#include
std::stable_sort(sv.begin(), sv.end(), std::less<std::string>());
所有代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class PrintString {
public:
PrintString (std::ostream &o = std::cout, char c = ' ') : os(o), sep(c) { }
void operator() (const std::string &s) const { os << s << sep; }
private:
std::ostream &os;
char sep;
};
/*Function Predicate*/
bool isLarger (const std::string &s1, const std::string &s2) {
return s1.size() > s2.size();
}
/*Function Object*/
class LargerString {
public:
bool operator() (const std::string& a, const std::string& b) {
return a.size() > b.size();
}
};
int main (void) {
std::vector<std::string> sv = {"Beauty", "Girl", "Lady", "Women", "Pretty"};
std::stable_sort(sv.begin(), sv.end(), isLarger);
std::cout << "Function Predicate : ";
std::for_each(sv.begin(), sv.end(), PrintString(std::cout));
std::cout << std::endl;
std::stable_sort(sv.begin(), sv.end(),
[](const std::string& s1, const std::string& s2){ return s1.size()>s2.size(); });
std::cout << "Lambda Expression Predicate : ";
std::for_each(sv.begin(), sv.end(), PrintString(std::cout));
std::cout << std::endl;
bool (*pf) (const std::string &s1, const std::string &s2);
pf = &isLarger;
std::stable_sort(sv.begin(), sv.end(), *pf);
std::cout << "Function Pointer Predicate : ";
std::for_each(sv.begin(), sv.end(), PrintString(std::cout));
std::cout << std::endl;
std::stable_sort(sv.begin(), sv.end(), LargerString());
std::cout << "Function Object Predicate : ";
std::for_each(sv.begin(), sv.end(), PrintString(std::cout));
std::cout << std::endl;
std::stable_sort(sv.begin(), sv.end(), std::larger<std::string>());
std::cout << "Library-Defined Function Object Predicate : ";
std::for_each(sv.begin(), sv.end(), PrintString(std::cout));
std::cout << std::endl;
return 0;
}
输出:
Function Predicate : Beauty Pretty Women Girl Lady
Lambda Expression Predicate : Beauty Pretty Women Girl Lady
Function Pointer Predicate : Beauty Pretty Women Girl Lady
Function Object Predicate : Beauty Pretty Women Girl Lady
Library-Defined Function Object Predicate : Women Pretty Lady Girl Beauty