C++函数对象(仿函数)

简介: C++函数对象(仿函数)

一、概念

重载函数调用操作符的类,其对象常称为函数对象;

函数对象使用重载的()时,行为类似函数调用,也叫仿函数;

函数对象(仿函数)是一个类,不是一个函数;

二、函数对象的使用

1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值;

2.函数对象超出普通函数的概念,函数对象可以有自己的状态

3. 函数对象可以作为参数传递

//函数对象(仿函数)
/*
 * 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值;
 * 函数对象超出普通函数的概念,函数对象可以有自己的状态
 * 函数对象可以作为参数传递
 */
class MyAdd {
public:
    int operator()(int v1, int v2) {
        return v1 + v2;
    }
};
 
// 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值;
void test01() {
    MyAdd myAdd;
    cout << myAdd(10, 10) << endl;
}
 
// 函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint {
public:
    MyPrint() {
        this->count = 0;
    }
 
    void operator()(string test) {
        cout << test << endl;
        this->count++;
    }
 
    int count;//内部自己状态
};
 
void test02() {
    MyPrint myPrint;
    myPrint("hi");
    myPrint("hi");
    myPrint("hi");
    myPrint("hi");
    myPrint("hi");
    cout << "函数myPrint的调用次数:" << myPrint.count << endl;
}
 
// 函数对象可以作为参数传递
void doPrint(MyPrint &mp, string test) {
    mp(test);
}
 
void test03() {
    MyPrint myPrint;
    doPrint(myPrint, "Hello");
}

三、谓词

返回bool类型的仿函数称为谓词;

如果operator()接受一个参数,那么叫做一元谓词;

如果operator()接受两个参数,那么叫做二元谓词;

 
//仿函数,返回值类型bool数据类型,称为谓词
//一元谓词
class BreaterFive {
public:
    bool operator()(int val) {
        return val > 5;
    }
};
 
void test04() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    // 查找容器中,有没有大于5的数字
    // BreaterFive()匿名函数对象
    vector<int>::iterator it = find_if(v.begin(), v.end(), BreaterFive());
 
    if (it != v.end()) {
        cout << "找到了大于5的数字:" << *it << endl;
    } else {
        cout << "没有找到" << endl;
    }
 
}
 
//二元谓词
class MyCompare {
public:
    bool operator()(int v1, int v2) {
        return v1 > v2;
    }
};
 
void test05() {
    vector<int> v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    // 默认排序从小到大
    cout << "默认排序" << endl;
    sort(v.begin(), v.end());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    cout << "自定义排序,从大到小:" << endl;
    sort(v.begin(), v.end(), MyCompare());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

四、内建算数仿函数

 
//内建函数对象,算数仿函数
// negate 一元仿函数,取反仿函数
void test06() {
    negate<int> n;
    cout << n(50) << endl;
    // 20 加法
    cout << plus<int>()(10, 20) << endl;
    // -10 减法
    cout << minus<int>()(10, 20) << endl;
    // 200 乘法
    cout << multiplies<int>()(10, 20) << endl;
    // 0 除法
    cout << divides<int>()(10, 20) << endl;
    // 10 取模
    cout << modulus<int>()(10, 20) << endl;
}

五、内建关系仿函数

 
//内建函数对象,关系函数
void test07() {
    // 等于 0
    cout << equal_to<int>()(10, 20) << endl;
    // 不等于 1
    cout << not_equal_to<int>()(10, 20) << endl;
    // 大于 0
    cout << greater<int>()(10, 20) << endl;
    // 大于等于 0
    cout << greater_equal<int>()(10, 20) << endl;
    // 小于 1
    cout << less<int>()(10, 20) << endl;
    // 小于等于 1
    cout << less_equal<int>()(10, 20) << endl;
}
 
void test08() {
    vector<int> v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    // 默认排序从小到大
    cout << "默认排序" << endl;
    sort(v.begin(), v.end());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    cout << "内建函数排序,从大到小:" << endl;
    sort(v.begin(), v.end(), greater<int>());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

六、内建逻辑仿函数

 
//内建函数对象_逻辑仿函数
//逻辑非 logical_not
void test09() {
    vector<bool> v;
    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    v.push_back(false);
    for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    vector<bool> v2;
    v2.resize(v.size());
    //源数据开始位置、源数据结束位置、目标开始位置、转换函数
    transform(v.begin(),v.end(),v2.begin(),logical_not<bool>());
    for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

七、全部代码

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>//内件函数对象头文件
 
using namespace std;
//函数对象(仿函数)
/*
 * 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值;
 * 函数对象超出普通函数的概念,函数对象可以有自己的状态
 * 函数对象可以作为参数传递
 */
class MyAdd {
public:
    int operator()(int v1, int v2) {
        return v1 + v2;
    }
};
 
// 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值;
void test01() {
    MyAdd myAdd;
    cout << myAdd(10, 10) << endl;
}
 
// 函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint {
public:
    MyPrint() {
        this->count = 0;
    }
 
    void operator()(string test) {
        cout << test << endl;
        this->count++;
    }
 
    int count;//内部自己状态
};
 
void test02() {
    MyPrint myPrint;
    myPrint("hi");
    myPrint("hi");
    myPrint("hi");
    myPrint("hi");
    myPrint("hi");
    cout << "函数myPrint的调用次数:" << myPrint.count << endl;
}
 
// 函数对象可以作为参数传递
void doPrint(MyPrint &mp, string test) {
    mp(test);
}
 
void test03() {
    MyPrint myPrint;
    doPrint(myPrint, "Hello");
}
 
//仿函数,返回值类型bool数据类型,称为谓词
//一元谓词
class BreaterFive {
public:
    bool operator()(int val) {
        return val > 5;
    }
};
 
void test04() {
    vector<int> v;
    for (int i = 0; i < 10; ++i) {
        v.push_back(i);
    }
    // 查找容器中,有没有大于5的数字
    // BreaterFive()匿名函数对象
    vector<int>::iterator it = find_if(v.begin(), v.end(), BreaterFive());
 
    if (it != v.end()) {
        cout << "找到了大于5的数字:" << *it << endl;
    } else {
        cout << "没有找到" << endl;
    }
 
}
 
//二元谓词
class MyCompare {
public:
    bool operator()(int v1, int v2) {
        return v1 > v2;
    }
};
 
void test05() {
    vector<int> v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    // 默认排序从小到大
    cout << "默认排序" << endl;
    sort(v.begin(), v.end());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    cout << "自定义排序,从大到小:" << endl;
    sort(v.begin(), v.end(), MyCompare());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
 
//内建函数对象,算数仿函数
// negate 一元仿函数,取反仿函数
void test06() {
    negate<int> n;
    cout << n(50) << endl;
    // 20 加法
    cout << plus<int>()(10, 20) << endl;
    // -10 减法
    cout << minus<int>()(10, 20) << endl;
    // 200 乘法
    cout << multiplies<int>()(10, 20) << endl;
    // 0 除法
    cout << divides<int>()(10, 20) << endl;
    // 10 取模
    cout << modulus<int>()(10, 20) << endl;
}
 
//内建函数对象,关系函数
void test07() {
    // 等于 0
    cout << equal_to<int>()(10, 20) << endl;
    // 不等于 1
    cout << not_equal_to<int>()(10, 20) << endl;
    // 大于 0
    cout << greater<int>()(10, 20) << endl;
    // 大于等于 0
    cout << greater_equal<int>()(10, 20) << endl;
    // 小于 1
    cout << less<int>()(10, 20) << endl;
    // 小于等于 1
    cout << less_equal<int>()(10, 20) << endl;
}
 
void test08() {
    vector<int> v;
    v.push_back(10);
    v.push_back(40);
    v.push_back(20);
    v.push_back(30);
    v.push_back(50);
    // 默认排序从小到大
    cout << "默认排序" << endl;
    sort(v.begin(), v.end());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    cout << "内建函数排序,从大到小:" << endl;
    sort(v.begin(), v.end(), greater<int>());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
 
//内建函数对象_逻辑仿函数
//逻辑非 logical_not
void test09() {
    vector<bool> v;
    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    v.push_back(false);
    for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    vector<bool> v2;
    v2.resize(v.size());
    //源数据开始位置、源数据结束位置、目标开始位置、转换函数
    transform(v.begin(),v.end(),v2.begin(),logical_not<bool>());
    for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
 
int main() {
    // test01();
    // test02();
    // test03();
    // test04();
    // test05();
    // test06();
    // test07();
    // test08();
    test09();
    system("pause");
    return 0;
}
 

目录
相关文章
|
17小时前
|
程序员 编译器 C++
探索C++语言宝库:解锁基础知识与实用技能(类型变量+条件循环+函数模块+OOP+异常处理)
探索C++语言宝库:解锁基础知识与实用技能(类型变量+条件循环+函数模块+OOP+异常处理)
4 0
|
2天前
|
缓存 C++
详细解读C++常用库函数C函数库cstdio
详细解读C++常用库函数C函数库cstdio
|
2天前
详细解读C++char类型函数
详细解读C++char类型函数
|
2天前
|
C++
C++类和对象3
C++类和对象
8 0
|
2天前
|
存储 编译器 C++
C++类和对象2
C++类和对象
10 0
|
2天前
|
C++
C++类和对象1
C++类和对象
15 0
|
2天前
|
C++
C++函数的默认参数、占位符、重载
C++函数的默认参数、占位符、重载
9 0
|
2天前
|
C++
C++函数
C++函数
2 0
|
3天前
|
编译器 C++
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(下)
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(下)
5 0
|
3天前
|
存储 编译器 C++
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(中)
【C++航海王:追寻罗杰的编程之路】类与对象你学会了吗?(中)
5 0