版权所有, 禁止转载, 如有需要, 请站内联系.
本文地址: http://blog.csdn.net/caroline_wendy/article/details/15378055
算法谓词, 即标准库算法传递的参数, 可以指定算法的操作, 如std::sort, 默认是从小到大, 通过谓词可以修改从大到小.
本文包含基本的5种谓词模式: 函数,函数指针,lambda表达式,函数对象,库定义的函数对象.
通过传递函数名, 匹配二元谓词(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);
建立一个函数指针, 传入算法, 使用指针代替函数名, 用法类似函数谓词.
代码:
bool (*pf) (const std::string &s1, const std::string &s2); pf = &isLarger; std::stable_sort(sv.begin(), sv.end(), *pf);
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(); });
类中重载函数的调用"()", 使类可以被调用, 并且传入算法谓词中, 进行使用.
代码:
/*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());
使用标准库定义的函数对象, 充当算法中的谓词, 包含在#include<functional>,包含基本的算法和逻辑操作.
代码:
std::stable_sort(sv.begin(), sv.end(), std::less<std::string>());
所有方法代码(Eclipse CDT; GCC 4.7.1):
/* * CppPrimer.cpp * * Created on: 2013.11.11 * Author: Caroline */ /*eclipse cdt*/ #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