当前位置: 首页 > news >正文

【C++笔记】第二十五篇 内建函数对象

C++的内建函数对象

1. 内建函数对象

① STL内建来了一些函数对象:

  1. 算术仿函数
  2. 关系仿函数
  3. 逻辑仿函数

② 用法:

  1. 这些仿函数所产生的对象,用法和一般函数完全相同。
  2. 使用内建函数对象,需要引入头文件 #include

2. 算术仿函数

① 功能描述:实现四则运算。

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

③ 仿函数原型:

  1. t e m p l a t e < c l a s s T > T p l u s < T > template<class T> T plus<T> template<classT>Tplus<T> //加法仿函数
  2. t e m p l a t e < c l a s s T > T m i n u s < T > template<class T> T minus<T> template<classT>Tminus<T> //减法仿函数
  3. t e m p l a t e < c l a s s T > T m u l t i p l i e s < T > template<class T> T multiplies<T> template<classT>Tmultiplies<T> //乘法仿函数
  4. t e m p l a t e < c l a s s T > T d i v i d e s < T > template<class T> T divides<T> template<classT>Tdivides<T> //除法仿函数
  5. t e m p l a t e < c l a s s T > T m o d u l u s < T > template<class T> T modulus<T> template<classT>Tmodulus<T> //取模仿函数
  6. t e m p l a t e < c l a s s T > T n e g a t e < T > template<class T> T negate<T> template<classT>Tnegate<T> //取反仿函数

④ 使用内建函数对象时,需要引入头文件#include

#include<iostream>
using namespace std;
#include<functional>  //内建函数对象头文件

//内建函数对象 算术仿函数

//negate 一元仿函数 取反仿函数
void test01()
{
    negate<int>n;

    cout << n(50) << endl;
}

//plus 二元仿函数  加法
void test02()
{
    plus<int>p;  //写一个int就好了,不用写两个int,它默认是两个同类型的int相加

    cout << p(50,60) << endl;
}

int main() {

    test01();
    test02();

    system("pause");

    return 0;

}
运行结果:
 - -50
 - 110
 - 请按任意键继续. . .

3. 关系仿函数

① 功能描述:实现关系对比。

② 仿函数原型:

  1. t e m p l a t e < c l a s s T > b o o l e q u a l t o < T > template<class T> bool equal_to<T> template<classT>boolequalto<T> //等于
  2. t e m p l a t e < c l a s s T > b o o l n o t e q u a l t o < T > template<class T> bool notequal_to<T> template<classT>boolnotequalto<T> //不等于
  3. t e m p l a t e < > c l a s s T > b o o l g r e a t e r < T > template<>class T> bool greater<T> template<>classT>boolgreater<T> //大于
  4. t e m p l a t e < c l a s s T > b o o l g r e a t e r q u a l < T > template<class T> bool greater_qual<T> template<classT>boolgreaterqual<T> //大于等于
  5. t e m p l a t e < c l a s s T > b o o l l e s s < T > template<class T> bool less<T> template<classT>boolless<T> //小于
  6. t e m p l a t e < c l a s s T > b o o l l e s s e q u a l < T > template<class T>bool less_equal<T> template<classT>boollessequal<T> //小于等于

③ 关系仿函数中最常用的就是greater<>大于。

#include<iostream>
using namespace std;
#include<functional>  //内建函数对象头文件
#include<vector>
#include<algorithm>


//内建函数对象 关系仿函数
//大于 greater
class MyCompare
{
    bool operator()(int v1, int v2)
    {
        return v1 > v2;
    }
};

void test01()
{
    vector<int>v;

    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    for (vector<int>::iterator it = v.begin();it!=v.end();it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    //降序
    
    //方式一:
    //sort(v.begin(), v.end(), MyCompare());
    
    //方式二:
    sort(v.begin(), v.end(), greater<int>()); //greater<int>()为编译器给提供的函数对象,为内建的函数对象

    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

int main() {

    test01();

    system("pause");

    return 0;

}
运行结果:
 - 10 20 30 40 50
 - 50 40 30 20 10
 - 请按任意键继续. . .

4. 逻辑仿函数

① 功能描述:实现关系对比

② 仿函数原型:

  1. t e m p l a t e < c l a s s T > b o o l l o g i c a l a n d < T > template<class T> bool logical_and<T> template<classT>boollogicaland<T> //逻辑与
  2. t e m p l a t e < c l a s s T > b o o l l o g i c a l o r < T > template<class T> bool logical_or<T> template<classT>boollogicalor<T> //逻辑或
  3. t e m p l a t e < c l a s s T > b o o l l o g i c a l n o t < T > template<class T> bool logical_not<T> template<classT>boollogicalnot<T> //逻辑非

③ 逻辑仿函数实际应用较少,了解即可。

#include<iostream>
using namespace std;
#include<functional>  //内建函数对象头文件
#include<vector>
#include<algorithm>


//内建函数对象 逻辑仿函数
//逻辑非 logical_not

void test01()
{
    vector<bool>v;

    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    v.push_back(true);
    v.push_back(false);

    for (vector<bool>::iterator it = v.begin();it!=v.end();it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    //利用逻辑非 将容器v  搬运到 容器v2中,并执行取反操作
    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();

    system("pause");

    return 0;

}
运行结果:
 - 1 0 1 1 0
 - 0 1 0 0 1
 - 请按任意键继续. . .

相关文章:

  • 【C/自定义类型详解】——结构体(struct)、位段、枚举(enum)、联合(union)
  • CSDN 编程竞赛·第八期总结
  • 如何搭建网课搜题公众号
  • MySQL总结
  • 【Web UI自动化测试】Web UI自动化测试之PO篇(全网最全)
  • 什么是跨域
  • 智能优化算法:白鲸优化算法-附代码
  • 【初阶C++】细谈new和delete以及函数与类的模板
  • 【EffectiveC++】让自己习惯C++
  • Python实现websocket接口自动化测试
  • 【Flutter】【widget】Checkbox 和 CheckboxListTile 复选框快速学习一下
  • 二叉搜索树——C++
  • 开发和测试争抢环境?是时候进行多环境建设了
  • 【网络篇】第六篇——网络套接字编程(二)(UDP详解)
  • (最新版2022版)剑指offer之队列 栈题解
  • WinForm应用实战开发指南 - 教你如何实现表头的全选操作?
  • 刷题笔记之三(统计回文+连续最大和+查找组成一个偶数最接近的两个素数+把字符串转换成整数+不要二)
  • 猜数字小游戏(加强版)它来了
  • 学习笔记-常见安全设备渗透方法
  • 《计算机视觉基础知识蓝皮书》第10篇 模型部署基础
  • 新华社评论员:在推进中国式现代化的宽广舞台上绽放青春光彩
  • 研究完蚂蚁搬家,我好像明白了为什么我们总是堵车
  • 长三角铁路持续迎五一出行高峰:今日预计发送旅客418万人次
  • 杭州挂牌临平区两宗住宅用地,起始总价约11.02亿元
  • 五一“拼假”催热超长假期,热门酒店民宿一房难求
  • 深入贯彻中央八项规定精神学习教育中央指导组派驻地方和单位名单公布