STL-函数对象(仿函数)

简介: STL-函数对象(仿函数)

概念:

  • 重载函数调用操作符的类,其对象被称为函数对象
  • 函数对象使用重载的()时,行为类似函数调用,也叫仿函数

本质:

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

函数对象使用:

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递
#include<iostream>
#include<string>
using namespace std;
//函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
class myAdd
{
public:
    int operator()(int v1, int v2)
    {
        return v1 + v2;
    }
};
void test1()
{
    myAdd a1;
    cout << a1(1, 2) << endl;
}
//函数对象超出普通函数的概念,函数对象可以有自己的状态
class myPrint
{
public:
    myPrint()
    {
        this->count = 0;
    }
    void operator()(string str)
    {
        cout << str << endl;
        count++;
    }
    int count;
};
void test2()
{
    myPrint p1;
    p1("A");
    p1("A");
    p1("A");
    cout << p1.count << endl;
}
//函数对象可以作为参数传递
void doPrint(myPrint& mp, string str)
{
    mp(str);
}
void test3()
{
    myPrint mp;
    doPrint(mp, "hello");
}
int main()
{
    test1();
    test2();
    test3();
    return 0;
}

谓词

  • 返回值是bool类型的仿函数被称为谓词
  • 如果operator()接受一个参数,那么叫做一元谓词
  • 如果operator()接受两个参数,那么叫做二元谓词

一元谓词举例

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class GreaterFive
{
public:
    bool operator()(int val)
    {
        return val > 5;
    }
};
int main()
{
    vector<int>v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    //查找容器中大于5的元素
    //GreaterFive() 匿名函数对象
    vector<int>::iterator it=find_if(v.begin(), v.end(), GreaterFive());
    cout << *it << endl;
    return 0;
}

二元谓词举例

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class myCompare
{
public:
    bool operator()(int v1, int v2)
    {
        return v1 > v2;
    }
};
int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    //默认sort升序排序
    sort(v.begin(), v.end());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    sort(v.begin(), v.end(), myCompare());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    return 0;
}

内建函数对象

STL内建了一些函数对象:

  • 算术仿函数
  • 关系仿函数
  • 逻辑仿函数

用法:

  • 这些仿函数所产生的对象,用法和一般函数完全相同
  • 使用内建仿函数,需要引入头文件<functional>

算术仿函数

其中negate是一元运算,其他都是二元运算:

  • template<class T> T puls<T>//加法
  • template<class T> T minus<T>//减法
  • template<class T> T multiplies<T>//乘法
  • template<class T> T modulus<T>//除法
  • template<class T> T negate<T>//取反
#include<iostream>
#include<functional>
using namespace std;
int main()
{
    //取反
    negate<int>n;
    cout << n(50) << endl;//-50
    cout << n(0) << endl;//0
    //加法 只填一个模板参数,默认相同类型
    plus<int>p;
    cout << p(10, 20) << endl;
    return 0;
}

关系仿函数

  • 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>//小于等于

sort默认的排序规则是从小到大,底层默认使用了less<T>

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(40);
    v.push_back(20);
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << endl;
    }
    sort(v.begin(), v.end(), less<int>());
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << endl;
    }
    return 0;
}

逻辑仿函数

  • template<class T> bool logical_and<T>逻辑与
  • template<class T> bool logical_or<T>逻辑或
  • template<class T> bool logical_not<T>逻辑非
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
    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;
    //将容器v搬运到容器v1中,并执行取反操作
    vector<bool>v2;
    v2.resize(v.size());
    //注意没有 v2.end()
    transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
    for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
    {
        cout << *it << " ";
    }
    return 0;
}
目录
相关文章
|
4月前
|
C++ 容器
|
3月前
|
C++
C++函数对象(仿函数)
C++函数对象(仿函数)
|
4月前
|
C++
C++模板与STL【函数对象】
C++模板与STL【函数对象】
|
存储 C++ 索引
1.1 C++ STL 字符串构造函数
String 字符串操作容器是C++标准中实现的重要容器,其主要用于对字符串的高效处理,它和C风格中的`string.h`并不是同一个库,两个库有极大的差距,C库中的`string.h`主要面向过程提供一些处理函数,而C++库中的`string`则是基于类实现的更高效的一种字符串处理方法集,类中提供了非常方便的成员函数供我们使用.
39 0
|
10月前
|
C++
C++初识仿函数
C++初识仿函数
|
编译器 C++ 容器
【C++】仿函数(less)
【C++】仿函数(less)
73 0
|
算法 区块链 C++
迭代器与仿函数
迭代器与仿函数
|
算法 搜索推荐 C++
第九层(10):STL之函数对象
第九层(10):STL之函数对象
第九层(10):STL之函数对象
|
编译器 C语言 C++
【STL实用技巧】函数对象(仿函数)与pair类型初探
【STL实用技巧】函数对象(仿函数)与pair类型初探
92 0
【STL实用技巧】函数对象(仿函数)与pair类型初探
|
C++
【C++函数对象】STL基础语法学习 | 仿函数&谓词&内建仿函数
重载函数调用操作符的类,其对象常称为函数对象。函数对象使用重载的()时,行为类似函数的调用,所以也叫仿函数。它的本质为一个类,而不是一个函数。
109 0
【C++函数对象】STL基础语法学习 | 仿函数&谓词&内建仿函数