1.函数对象
-
函数对象(仿函数):
重载函数调用操作的类,其对象常称之为函数对象;
函数对象使用重载()时,其行为类似函数调用,也叫仿函数; - 函数对象本质:
函数对象(仿函数)本质是一个类,不是一个函数。
- 函数对象特点:
函数对象在使用时可以有形参、有返回值。
函数对象可以有自己的状态值。
函数对象可以作为函数形参。
使用示例:
#include < iostream >
using namespace std;
class myfunc
{
public:
myfunc()
{
count = 0;
}
//求和示例,重载()
int operator()(int a, int b)
{
return a + b;
}
//输出示例,count记录函数调用次数
void operator()(string str)
{
count++;
cout < < str < < endl;
}
int count;
};
void print(myfunc& p, string test)
{
p(test);
}
void test()
{
//创建一个函数对象
myfunc p1;
cout < < "t函数对象形参返回使用示例:" < < endl;
int ret=p1(10, 20);
cout < < "ret=" < < ret < < endl;
cout < < "t仿函数重载示例:" < < endl;
p1("C++学习--仿函数使用示例!");
p1("C++学习--仿函数使用示例!");
p1("C++学习--仿函数使用示例!");
cout < < "函数调用次数:" < < p1.count < < endl;
cout < < "t仿函数作为函数形参:" < < endl;
print(p1, "hello,欢迎学习c++课程");
}
int main()
{
test();
system("pause");
}

2.谓词
-
谓词:
函数对象返回值为bool类型,则称之为谓词; -
一元谓词:
仿函数的形参只有一个; -
二元谓词:
仿函数的形参有两个参数;
#include < iostream >
#include < vector >
#include < algorithm >
using namespace std;
class Check
{
public:
bool operator()(int val)
{
return val > 5;
}
bool operator()(int a1,int a2)
{
return a1 > a2;
}
};
void test()
{
vector< int >vtr;
/*插入数据*/
for (int i = 0; i < 10; i++)
{
vtr.push_back(i);
}
cout < < "一元谓词示例:查找vector容器中 >5的值" < < endl;
/*查找vector容器中 >5的值*/
vector< int >::iterator ret=find_if(vtr.begin(), vtr.end(), Check());//Check() ---匿名函数对象
if (ret ==vtr.end())
{
cout < < "未查到到 >5的值!" < < endl;
}
else
{
cout < < "查找成功,大于5的值为:" < <*ret< ::iterator ptr = vtr.begin(); ptr != vtr.end(); ptr++)
{
cout < < *ptr < < " ";
}
cout < < endl;
}
int main()
{
test();
system("pause");
}
;>

3.内建函数对象
-
内建函数对象:
STL中提供了一些内建函数对象:算术仿函数、关系仿函数、逻辑仿函数 --头文件
3.1算术运算符
- 算术仿函数:实现四则运算。
加法:template T plus
减法:template T minus
乘法:template T mutiplies
除法:template T divides
取模:template T modulus
取反:template T negate --正数变负数,负数变正数
注意:其中negate是一元运算(只有一个参数),其余均为二元运算。
#include < iostream >
using namespace std;
#include < functional >
void test()
{
//negate使用示例:
negate< int > n;
cout < < "negate取反示例:" < < n(188) < < endl;
plus< int > p;
cout < < "plus加法:" < < p(10, 20) < < endl;
minus< float >m;
cout < < "minus减法取绝对值:" < < n(m(10, 20)) < < endl;
multiplies< float >mt;
cout < < "multiplies乘法:" < < mt(5, 3.15) < < endl;
divides< float >d;
cout < < "divides除法:" < < d(10, 3) < < endl;
modulus< int >md;
cout < < "modulus取模:" < < md(10, 3) < < endl;
}
int main()
{
test();
system("pause");
}
3.2关系运算符
- 内建仿函数:关系运算符
大于: templatebool greater
大于等于:templatebool greater_equal
小于: templatebool less
小于等于:templatebool less_equal
等于: templatebool equal_to
不等于: templatebool not_equal_to
#include < iostream >
using namespace std;
#include < functional >
#include < vector >
#include < algorithm >
void print(int val)
{
cout < < val < < " ";
}
int main()
{
vector< int > vtr;
vtr.push_back(10);
vtr.push_back(40);
vtr.push_back(30);
vtr.push_back(60);
vtr.push_back(6);
/*sort排序,默认是从小到大,其默认的仿函数即less*/
sort(vtr.begin(), vtr.end());
for_each(vtr.begin(), vtr.end(), print);
cout < < endl;
/*
要实现从大小,可以自行实现一个仿函数
class mycompare
{
public:
bool operator()(int a1,int a2)
{
return a1 >a2;
}
}
也可以直接使用STL内建仿函数:greater()
*/
sort(vtr.begin(), vtr.end(), greater< int >());
for_each(vtr.begin(), vtr.end(), print);
cout < < endl;
system("pause");
}

3.3逻辑运算符
- 内建仿函数--逻辑运算符
逻辑与:templatebool logical_and
逻辑或: templatebool logical_or
逻辑非: templatebool logical_not
#include < iostream >
using namespace std;
#include < vector >
#include < algorithm >
#include < functional >
void print(bool val)
{
cout < < val < < " ";
}
void test()
{
vector vtr;
vtr.push_back(true);
vtr.push_back(true);
vtr.push_back(false);
vtr.push_back(false);
vectorvtr2;
vtr2.resize(vtr.size());//设置vtr2的容器大小
//将vtr容器内容取非放到vtr2中
transform(vtr.begin(), vtr.end(), vtr2.begin(), logical_not());
for_each(vtr.begin(), vtr.end(), print);
cout < < endl;
for_each(vtr2.begin(), vtr2.end(), print);
cout < < endl;
}
int main()
{
test();
system("pause");
}

审核编辑:汤梓红
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。
举报投诉
-
函数
+关注
关注
3文章
4423浏览量
68034 -
C++
+关注
关注
22文章
2131浏览量
77485
发布评论请先 登录
相关推荐
热点推荐
【乾芯QXS320F开发板试用】TMUFPU内建函数
到现在,一层套一层,我又在QX-IDE使用手册里找到了内建函数描述表,这里面就是FPU和TMU使用的C函数
要使用内建函数,必须引用下图头文件 qx320f.h
打开这个头文件,里面都
发表于 12-11 13:32
GCC内建函数问题!!!
目前的代码中使用了GCC的内建函数__builtin_clz()和__builtin_popcount(),在CCS中编译不能通过,请问,CCS是否有库支持这两个函数,多谢!
发表于 06-21 18:30
C++课程资料详细资料合集包括了:面向对象程序设计与C++,算法,函数等
本文档的主要内容详细介绍的是C++课程资料资料合集包括了:面向对象程序设计与C++,算法,函数,概述, C++语言基础,构造数据类型,数据类
发表于 07-09 08:00
•18次下载
如何在中断C函数中调用C++
之前,我们在单片机程序开发时都会面对中断函数。众所周知的,这个中断函数肯定是要用C函数来定义的。我在用C++进行程序开发的时候就发现了一个需
发表于 05-09 18:17
•0次下载
C++基础语法之inline 内联函数
上节我们分析了C++基础语法的const,static以及 this 指针,那么这节内容我们来看一下 inline 内联函数吧! inline 内联函数 特征 相当于把内联函数里面的内
如何在MPLAB XC16编译器内建函数
MPLAB XC16编译器提供了一系列内建函数,使C语言编程人员可以访问目前只能通过行内汇编访问的汇编运算符或机器指令。内建函数名都有一个前缀__builtin_,源代码用C语言编写,
虚函数,C++开发者如何有效利用
虚函数是基类中声明的成员函数,且使用者期望在派生类中将其重新定义。那么,在 C++ 中,什么是虚函数呢?在 C++ 中,通常将虚
浅析C++执行构造函数编程实例
C++保证在你调用delete时,先自动调用析构函数(而我们安排在这个函数里面删除它的法杖、法袍等对象),再删除对象占用的内存。
发表于 03-03 15:44
•736次阅读
C++基础知识之函数1
函数是 C++ 中的一个重要概念,它可以让我们将一段代码封装起来,然后在需要的时候调用它。C++ 中的函数有以下几个特点:
* 函数可
C++基础知识之函数2
在C++中,我们可以使用inline关键字来定义内联函数。内联函数是一种特殊的函数,它在编译时会被直接嵌入到调用它的代码中,从而避免了函数调
c++之函数对象与内建函数
评论