STL函数对象基本使用
发布日期:2022-03-04 12:48:33 浏览次数:23 分类:技术文章

本文共 706 字,大约阅读时间需要 2 分钟。

黑马237

#include
#include
#include
#include
#include
using namespace std;
//函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
class myClass
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
//函数对象超出普通函数的概念,函数对象可以有自己的状态
class myPrint
{
public:
myPrint()
{
this->count = 0;
}
void operator()(string test)
{
cout << test << endl;
count++;
}
int count;//拥有自己的状态
};
void test01()
{
myClass add;
cout << add(10, 10) << endl;
}
void test02()
{
myPrint myprint;
myprint(“hello”);
myprint(“hello”);
myprint(“hello”);
cout << “调用次数:” << myprint.count << endl;
}
//函数对象可以作为参数传递
void doPrint(myPrint& mp, string test)
{
mp(test);
}
void test03()
{
myPrint myprint;
doPrint(myprint, “hello C++”);
}
int main()
{
test03();
}

转载地址:https://blog.csdn.net/qq_43752920/article/details/120533420 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:STL案例2-成员分组
下一篇:谓词 一元谓词

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年03月26日 10时25分01秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

c语言对结构体排序中间变量,求助:c语言怎么实现结构体的排序? 总是弄不对啊... 2019-04-21
c语言宏定义只能在最前面吗,C语言宏定义注意事项 2019-04-21
android悬浮窗服务卡死,Android 悬浮窗兼容问题谈 2019-04-21
表格相关的html语言,HTML标记语言——表格标记 2019-04-21
web聊天界面html,PC端Web聊天界面+代码分享(HTML+CSS) 2019-04-21
cmake qt 添加路径 项目_CMake配置Qt工程 2019-04-21
使用python开发的软件协议_WEB开发——Python WSGI协议详解 2019-04-21
冰点下载器手机版apk_冰点文库下载器 2019-04-21
python信号采集代码_13行代码实现:Python实时视频采集(附源码) 2019-04-21
h5引入json_纯js直接引入json文件 2019-04-21
python格式化字符串总结_Python字符串处理方法总结 2019-04-21
python中true什么意思_python中的bool是什么意思 2019-04-21
jacobian 矩阵意义_Jacobian矩阵和Hessian矩阵的作用是什么? 2019-04-21
c++ jna 数据类型_JNA 使用总结 2019-04-21
python中如何遍历列表并将列表值赋予_python中如何实现遍历整个列表? 2019-04-21
apache php mysql架构图_Apache+PHP+MYSQL+Tomcat+JK架构设计技巧与应用实战 2019-04-21
xlnt库如何编译_最新mysql数据库源码编译安装。 2019-04-21
mysql 2003错误 10055_MYSQL无法连接---提示10055错误 2019-04-21
mysql redis缓存层_redis实现缓存的两种方式 2019-04-21
git 改local branch名字_用Git管理Latex写论文的工作流程 2019-04-21