C++ STL(29):Function Object Adapter(函数对象适配器)
2018-03-30 10:51
771 查看
#include <iostream> #include <list> #include <algorithm> #include <iterator> #include <functional> #include <cstring> //Function Object Adapter int main() { /************************************************************************/ //binder1st:绑定第一个参数 /* template<class Operation, class Type> binder1st <Operation> bind1st( const Operation& _Func, const Type& _Left ); */ /************************************************************************/ std::list<int> L; std::generate_n(std::front_inserter(L), 10, []{return rand() % 10; }); std::copy(L.begin(), L.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; using Iterator = std::list<int>::iterator; Iterator it = std::find_if(L.begin(), L.end(), std::bind1st(std::equal_to<int>(), 3)); if (it == L.end()) std::cout << "Not found! " << std::endl; else std::cout << "Found the first 3 in index: " << std::distance(it, L.begin()) << std::endl; /************************************************************************/ //binder2nd:绑定第二个参数 /* template<class Operation, class Type> binder2nd <Operation> bind2nd( const Operation& _Func, const Type& _Right ); */ /************************************************************************/ it = std::find_if(L.begin(), L.end(), std::bind2nd(std::less_equal<int>(), 3)); if (it == L.end()) std::cout << "Not found! " << std::endl; else std::cout << "Found the less than 3 in index: " << std::distance(L.begin(), it) << std::endl; /************************************************************************/ //pointer_to_unary_function和pointer_to_binary_function不直接使用,应该使用辅助函数ptr_fun来代替 /* template<class Arg, class Result> class pointer_to_unary_function : public unary_function<Arg, Result> { public: explicit pointer_to_unary_function( Result(*_pfunc)(Arg) ); Result operator()( Arg _Left ) const; }; */ /* template<class Arg1, class Arg2, class Result> class pointer_to_binary_function : public binary_function <Arg1, Arg2, Result> { public: explicit pointer_to_binary_function( Result(*_pfunc)(Arg1, Arg2) ); operator()( Arg1 _Left, Arg2 _Right ) const; }; */ /* template<class Arg, class Result> pointer_to_unary_function<Arg, Result, Result(*)(Arg)> ptr_fun(Result(*_pfunc)(Arg)); template<class Arg1, class Arg2, class Result> pointer_to_binary_function<Arg1, Arg2, Result, Result(*)(Arg1, Arg2)> ptr_fun(Result(*_pfunc)(Arg1, Arg2)); */ /************************************************************************/ std::list<char*> lc; lc.push_back("apple"); lc.push_back("pear"); lc.push_back("orange"); lc.push_back("banana"); using It_char = std::list<char*>::iterator; It_char ic = std::find_if(lc.begin(), lc.end(), std::not1(std::bind2nd(std::ptr_fun(strcmp), "orange"))); if (ic != lc.end()) std::cout << "The next of orange is " << *++ic << std::endl; /************************************************************************/ //unary_negate和binary_negate不直接使用,使用辅助函数not1和not2来代替 /* template<class Predicate> class unary_negate : public unary_function< typename Predicate::argument_type, bool> { public: explicit unary_negate( const Predicate& _Func ); bool operator()( const typename Predicate::argument_type& _Left) const; }; */ /* template<class Operation> class binary_negate : public binary_function < typename Operation::first_argument_type, typename Operation::second_argument_type, bool> { public: explicit binary_negate( const Operation& _Func ); bool operator()( const typename Operation::first_argument_type& _Left, const typename Operation::second_argument_type& _Right ) const; }; */ /************************************************************************/ //略 return 0; }====================打个广告,欢迎关注====================
QQ: | 412425870 |
csdn博客: | http://blog.csdn.net/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: 328243383(1群) | |
点击群号或者扫描二维码即可加入QQ群:180479701(2群) |
相关文章推荐
- c++ stl Function Objects 函数对象
- C++ STL(25):Function Object Classes(函数对象类)
- Function Objects Adapter(函数对象适配器)
- C++ STL/ (12) 函数对象适配器
- C++ 之使用标准库函数对象和函数适配器
- STL运用的C++技术(6)——函数对象
- STL的6大组件:容器、类属算法、迭代器、函数对象、适配器、分配器。
- C++STL之函数对象及谓词
- STL-函数对象及函数对象适配器
- Javascript中的函数(Function)与对象(Object)的关系
- Javascript中的函数(Function)与对象(Object)的关系
- function adapter(函数适配器)和迭代器适配器
- 从零开始学C++之STL(八):函数对象、 函数对象与容器、函数对象与算法
- C++ STL 函数对象简介
- C++ STL 学习笔记 函数对象
- Javascript中的函数(Function)与对象(Object)的关系
- 函数对象状态(Function Object State)获取 -- 引用传递和for_each()
- C++手稿:STL中的函数对象与函数指针
- STL知识点(常用算法函数介绍 、容器、类属算法、迭代器、函数对象、适配器、分配器
- STL函数对象及函数对象适配器