STL-函数对象、仿函数

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class MyAdd
{
public:
    MyAdd()
    {
        count = 0;
    }

    int operator()(int v1, int v2)
    {
        ++count;
        return v1 + v2;
    }

    int count; //有自己的状态
};

//3. 函数对象可以作为参数传递
void PrintCount(MyAdd &add)
{
    cout << add.count << endl;
}

void test01()
{
    //1. 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
    MyAdd myadd;
    cout << myadd(10, 20) << endl;
    myadd(2, 3);
    myadd(3, 4);

    //2. 函数对象可以有自己的状态
    cout << myadd.count << endl;

    //3. 函数对象可以作为参数传递
    PrintCount(myadd);
}

/*-------------------------------------------------------------------------------*/

//一元谓词
class GreaterFive
{
public:
    bool operator()(int val)
    {
        return val > 5;
    }
};

//二元谓词
class MyCompare
{
public:
    bool operator()(int v1, int v2)
    {
        return v1 > v2;
    }
};

void test02()
{
    vector<int> v1{ 3, 1, 6, 8, 2, 5};
    for_each(v1.begin(), v1.end(), [&](int val) {
        cout << val << " ";
    });
    cout << endl;

    auto it = find_if(v1.begin(), v1.end(), GreaterFive());
    if (it == v1.end())
    {
        cout << "没有大于5的数!" << endl;
    }
    else
    {
        cout << "第一个大于5的数:" << *it << endl;
    }

    //使用函数对象改变排序规则
    /*
    sort(v1.begin(), v1.end(), [](int v1, int v2) {
        return v1 > v2;
    });
    */
    sort(v1.begin(), v1.end(), MyCompare());
    for_each(v1.begin(), v1.end(), [&](int val) {
        cout << val << " ";
    });
    cout << endl;
}

/*-------------------------------------------------------------------------------*/

int main(int argc, char const *argv[])
{
    /*1. 重载函数调用操作符的类,其对象成为函数对象
    2. 函数对象使用重载的()时,行为类似函数调用,也叫仿函数
    3. 函数对象(仿函数)本质是一个类,不是函数
    特点:
        1). 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
        2). 函数对象超出普通函数的概念,可以有自己的状态
        3). 函数对象可以作为参数传递
    */
    test01();

    /*1. 返回bool类型的仿函数成为谓词
    2. 如果operator()接受一个参数,那么叫做一元谓词
    3. 如果operator()接受两个参数,那么叫做二元谓词
    */
    test02();
    

    return 0;
}
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;

//算数仿函数
void test01()
{
    /*
    template<class T> T plus<T> //加法仿函数
    template<class T> T minus<T> //减法仿函数
    template<class T> T multiplies<T> //乘法仿函数
    template<class T> T divides<T> //除法仿函数
    template<class T> T modulus<T> //取模仿函数
    template<class T> T negate<T> //取反仿函数
    */
    negate<int> n;
    cout << n(50) << endl;

    plus<int> p;
    cout << p(10, 20) << endl;
}

//关系仿函数
void test02()
{
    /*
    template<class T> bool equal_to<T> //等于
    template<class T> bool not_equal_to<T> //不等于
    template<class T> bool greater<T> //大于
    template<class T> bool greater_equal<T> //大于等于
    template<class T> bool less<T> //小于
    template<class T> bool less_equal<T> //小于等于
    */
    vector<int> v1{3, 5, 2, 7, 1, 9, 6};
    sort(v1.begin(), v1.end(), greater<int>());
    for_each(v1.begin(), v1.end(), [&](int val) {
        cout << val << " ";
    });
    cout << endl;
}

//逻辑仿函数
void test03()
{
    /*
    template<class T> bool logical_and<T> //逻辑与
    template<class T> bool logical_or<T> //逻辑或
    template<class T> bool logical_not<T> //逻辑非
    */
    vector<bool> v1{true, true, false, false};
    for (vector<bool>::iterator it = v1.begin(); it != v1.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;

    vector<bool> v2;
    v2.resize(v1.size());
    transform(v1.begin(), v1.end(), v2.begin(), logical_not<bool>());
    for (vector<bool>::iterator it = v2.begin(); it != v2.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;
}

int main(int argc, char const *argv[])
{
    /*
    1. STL内建了一些函数对象:算数仿函数,关系仿函数,逻辑仿函数...
    2. 这些仿函数所产生的对象,用法和一般函数完全相同
    */

    //1. 算数仿函数  实现四则运算,negate是一元运算,其他都是二元运算
    test01();

    //2. 关系运算符  实现关系对比
    test02();

    //3. 逻辑仿函数  实现逻辑运算
    test03();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值