您的位置:首页 > 移动开发 > Objective-C

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群)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Cpp STL