首页 > 编程语言 >c++内建函数对象

c++内建函数对象

时间:2024-03-13 15:22:21浏览次数:18  
标签:函数 对象 back c++ 内建函数 template printVector push include

概念:c++STL中内建了一些函数对象

分类:

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

用法:

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

 

1.算术仿函数

 功能描述:

  • 实现四则运算
  • 其中negate是一元运算,其他都是二元运算

仿函数原型:

  • 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>      //取反仿函数

实例代码:

#include <iostream>

using namespace std;

#include <functional>

void test01() {
    //negate一元仿函数,取反
    negate<int> v;
    cout << v(50) << endl;
}

void test02() {
    //plus二元仿函数,加法
    plus<int> v;
    cout << v(10, 20) << endl;
}

int main() {
    //结果:-50 30
    test01();
    test02();
}

 

 

2.关系仿函数

功能描述:

  • 实现关系对比

仿函数原型:

  • template<class T> bool equal_to<T>         //等于
  • template<class T> bool not_equal_to<T>   //不等于
  • template<class T> bool greater<T>           //大于
  • template<class T> gretaer_equal<T>         //大于等于
  • template<class T> less<T>                      //小于
  • template<class T> less_equal<T>             //小于等于

实例代码:

#include <iostream>
#include <vector>

using namespace std;

#include <functional>

//自定义仿函数
class myCompare {
public:
    bool operator()(int v1, int v2) {
        return v1 > v2;
    }
};

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

void test01() {
    vector<int> v;
    v.push_back(40);
    v.push_back(20);
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);

    printVector(v);

    //升序排列
    sort(v.begin(), v.end());
    printVector(v);

    //降序排列,使用自定义的仿函数
    sort(v.begin(), v.end(), myCompare());
    printVector(v);

    //降序排列,使用内建的仿函数
    sort(v.begin(), v.end(), greater<int>());
    printVector(v);

}

int main() {
    test01();
}

结果:

40 20 10 30 50
10 20 30 40 50
50 40 30 20 10
50 40 30 20 10

 

3.逻辑仿函数

功能描述:

  • 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>

using namespace std;

#include <functional>

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

void test01() {
    vector<bool> v;
    v.push_back(true);
    v.push_back(false);
    v.push_back(true);
    v.push_back(false);

    printVector(v);

    //使用逻辑非,将容器v搬运到容器v2中,并执行取反操作
    vector<bool> v2;
    v2.resize(v.size());
    transform(v.begin(),v.end(),v2.begin(),logical_not<bool>());

    printVector(v2);

}

int main() {
    test01();
}

结果:

1 0 1 0
0 1 0 1

标签:函数,对象,back,c++,内建函数,template,printVector,push,include
From: https://www.cnblogs.com/eisenshu/p/18070543

相关文章

  • Promise对象的特点
    Promise对象的特点·:对象的状态不受外界影响。Promise对象代表一个异步操作,有三种状态:pending(进行中)fulfilled(已成功)rejected(已失败)只有异步操作的结果,可以决定当前是哪一种状态,任何其他操作都无法改变这个状态。·一旦状态改变,就不会再变,任何时候都可以得到这个结果。......
  • 【Javascript】 Promise 对象(一)
    Promise的含义Promise是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大。它由社区最早提出和实现,ES6将其写进了语言标准,统一了用法,原生提供了Promise对象。所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操......
  • C++ cout的使用总结
    cout是C++中ostream类型的对象cout是C++中ostream类型的对象,该类被封装在<iostream>库中,该库定义的名字都在命名空间std中,所以cout全称是std::cout。1、cout支持多种数据类型,如int、float、double、char、string等,它们都会被自动转换成字符串进行输出。#includ......
  • 十五届蓝桥青少C++组3月评测2024年3月中高级
    STEMA考试C++中高级试卷(24年3月10日)一、选择题(50分)1:(110010)2+(c3)16的结果是()。*选择题严禁使用程序验证,选择题不答或答错都不扣分A.(240)10 B.(11110101)2 C.(366)8 D.(f6)16 备注:此题目下标代表进制,因不支持md格式。 参考答案:B2:表达式1000/3的结果......
  • C++面试100问!(三)
    前言    本次专题旨在回顾C++的一些基础,方便实时总结。C++源文件从文本到可执行文件经历的过程?         预处理阶段:对源代码文件中文件包含关系(头文件)、预编译语句(宏定义)进行分析和替换,生成预编译文件。        编译阶段:将经过预处理后的预编译文......
  • [C++]C++函数指针总结
    指针的概念指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址。要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占据的内存区。让我们分别说明。先声明几个指针放着做例子:例一:int*......
  • [C++]c++ 在项目cpp文件中直接用#pragma comment语句引入,无需配置属性表
    使用语句添加引入库的好处就是无需配置多个属性表,不管是debug模式还是release模式,不管是64位还是32位,几行代码通吃。相对路径:#ifdef_M_X64#pragmacomment(lib,"../../../VC++/DVPCamera64.lib")#else#pragmacomment(lib,"../../../VC++/DVPCamera32.lib")#endif绝......
  • C++图书管理案例
    Book类存储一本图书信息。classBook{public:stringbookId;stringtitle;floatprice;//构造函数Book(stringmyBookId,stringmyTitle,floatmyPrice){bookId=myBookId;title=myTitle;price=myPrice;......
  • c++函数SetConsoleTextAttribute
    前言正文1.作用:2.函数格式(重点):3.参数(重点):前言实用(真的)正文1.作用:更改cmd的背景色与字体颜色2.函数格式(重点):SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),10进制参数);3.参数(重点): ......
  • C++指针(五)完结篇
    个人主页:PingdiGuo_guo收录专栏:C++干货专栏前言相关文章:C++指针(一)、C++指针(二)、C++指针(三)、C++指针(四)万字图文详解!本篇博客是介绍const、野指针、双指针、assert断言、NULL指针的知识的。文章目录前言1.const与指针1.1修饰变量1.2修饰指针变量2.野指针2.1为......