【C++笔记】第二十五篇 内建函数对象
创始人
2024-04-03 01:59:33
0

C++的内建函数对象

1. 内建函数对象

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

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

② 用法:

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

2. 算术仿函数

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

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

③ 仿函数原型:

  1. templateTplustemplate T plustemplateTplus //加法仿函数
  2. templateTminustemplate T minustemplateTminus //减法仿函数
  3. templateTmultipliestemplate T multipliestemplateTmultiplies //乘法仿函数
  4. templateTdividestemplate T dividestemplateTdivides //除法仿函数
  5. templateTmodulustemplate T modulustemplateTmodulus //取模仿函数
  6. templateTnegatetemplate T negatetemplateTnegate //取反仿函数

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

#include
using namespace std;
#include  //内建函数对象头文件//内建函数对象 算术仿函数//negate 一元仿函数 取反仿函数
void test01()
{negaten;cout << n(50) << endl;
}//plus 二元仿函数  加法
void test02()
{plusp;  //写一个int就好了,不用写两个int,它默认是两个同类型的int相加cout << p(50,60) << endl;
}int main() {test01();test02();system("pause");return 0;}
运行结果:- -50- 110- 请按任意键继续. . .

3. 关系仿函数

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

② 仿函数原型:

  1. templateboolequaltotemplate bool equal_totemplateboolequalt​o //等于
  2. templateboolnotequaltotemplate bool notequal_totemplateboolnotequalt​o //不等于
  3. template<>classT>boolgreatertemplate<>class T> bool greatertemplate<>classT>boolgreater //大于
  4. templateboolgreaterqualtemplate bool greater_qualtemplateboolgreaterq​ual //大于等于
  5. templateboollesstemplate bool lesstemplateboolless //小于
  6. templateboollessequaltemplatebool less_equaltemplateboollesse​qual //小于等于

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

#include
using namespace std;
#include  //内建函数对象头文件
#include
#include//内建函数对象 关系仿函数
//大于 greater
class MyCompare
{bool operator()(int v1, int v2){return v1 > v2;}
};void test01()
{vectorv;v.push_back(10);v.push_back(20);v.push_back(30);v.push_back(40);v.push_back(50);for (vector::iterator it = v.begin();it!=v.end();it++){cout << *it << " ";}cout << endl;//降序//方式一://sort(v.begin(), v.end(), MyCompare());//方式二:sort(v.begin(), v.end(), greater()); //greater()为编译器给提供的函数对象,为内建的函数对象for (vector::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. templateboollogicalandtemplate bool logical_andtemplateboollogicala​nd //逻辑与
  2. templateboollogicalortemplate bool logical_ortemplateboollogicalo​r //逻辑或
  3. templateboollogicalnottemplate bool logical_nottemplateboollogicaln​ot //逻辑非

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

#include
using namespace std;
#include  //内建函数对象头文件
#include
#include//内建函数对象 逻辑仿函数
//逻辑非 logical_notvoid test01()
{vectorv;v.push_back(true);v.push_back(false);v.push_back(true);v.push_back(true);v.push_back(false);for (vector::iterator it = v.begin();it!=v.end();it++){cout << *it << " ";}cout << endl;//利用逻辑非 将容器v  搬运到 容器v2中,并执行取反操作vectorv2;v2.resize(v.size()); //目标容器要提前开辟一个空间transform(v.begin(),v.end(),v2.begin(),logical_not()); //第一个参数:原容器起始迭代器,第二个参数:原容器终止迭代器,第三个参数:目标容器起始迭代器for (vector::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- 请按任意键继续. . .

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...