#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
//Function Object Classes:函数对象类
int main()
{
/************************************************************************/
//unary_function:一元函数类
/*
template<class _Arg, class _Result>
struct unary_function
{
typedef _ArgArgument_Type;
typedef _Result Result_Type;
};
*/
/************************************************************************/
class/*struct*/ SinFunction : public std::unary_function<double, double>
{
public:
result_type operator()(argument_type d)const
{
return sin(d);
}
};
const double M_PI = 3.14159265358979323846;
double PI_Array[] = {M_PI / 2, M_PI / 3, M_PI / 4, M_PI / 6 };
std::for_each(std::begin(PI_Array), std::end(PI_Array), [](const double& each)
{
std::cout << SinFunction()(each) << std::endl;
});
/************************************************************************/
//binary_function:二元函数类
/*
template<class _Arg1, class _Arg2, class _Result>
struct binary_function
{
typedef _Arg1first_argument_type;
typedef _Arg2second_argument_type;
typedef _Result result_type;
};
*/
/************************************************************************/
struct /*class*/ Pow : public std::binary_function<double, double, double>
{
result_type operator()(first_argument_type arg1, second_argument_type arg2)const
{
return pow(arg1, arg2);
}
};
typedef struct tagExp
{
double bottom;
double top;
}Exp;
Exp exps[] = { { 1.0, 1.0 }, { 2.0, 2.0 }, { 3.0, 3.0 } };
//为了使用pair,使得程序复杂化了
std::vector<std::pair<double, double>> pv;
std::for_each(std::begin(exps), std::end(exps), [&](const Exp& e)
{
pv.push_back(std::make_pair(e.bottom, e.top));
});
std::for_each(pv.begin(), pv.end(), [](const std::pair<double, double>& p)
{
std::cout << Pow()(p.first, p.second) << std::endl;
});
return 0;
}
====================打个广告,欢迎关注====================
QQ: | 412425870 |
csdn博客: | http://blog.csdn.net/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | |