網(wǎng)站首頁 編程語言 正文
1函數(shù)對象Functor(仿函數(shù))
1.1概念
函數(shù)對象就是類對象,生成這個類對象的類中,擁有一個小括號運(yùn)算符重載函數(shù)。
重載了小括號運(yùn)算符的類的類對象,就叫函數(shù)對象。
1.2代碼實(shí)例
#include <iostream>
using namespace std;
template <class T1>
class A
{
T1 name;
public:
A(T1 name)
{
this->name=name;
}
void operator()()
{
cout<<this->name<<endl;
}
};
int main()
{
A<string> a("da jia hao");
a();
return 0;
}
1.3調(diào)用效率
函數(shù)對象相較于普通函數(shù)與函數(shù)指針的調(diào)用效率要高很多
當(dāng)我們用普通函數(shù)的時候:像下面這種情況,每次都需要回調(diào),特別的繁瑣。
注意:在這個代碼里面,即使我們在函數(shù)前面都加上inline變成內(nèi)聯(lián)函數(shù),也是不行的,因?yàn)槲覀兪褂玫氖呛瘮?shù)指針,即使變成了內(nèi)聯(lián)函數(shù),我們還是需要回調(diào)。
#include <iostream>
using namespace std;
template <class T>
T my_bigger(T a,T b)
{
return a>b?a:b;
}
template <class T>
T my_less(T a,T b)
{
return a>b?b:a;
}
template <class T,class Compare>
T my_compare(T a, T b,Compare f)
{
return f(a,b);
}
int main()
{
cout<< my_compare<int>(10,20,my_less<int>)<<endl;
cout<<my_compare<int>(10,20,my_bigger<int>)<<endl;
return 0;
}
使用函數(shù)對象就可以解決這個問題,因?yàn)槭褂煤瘮?shù)對象就相當(dāng)于在函數(shù)面前加上一個inline,效率會提升,代碼如下:
#include <iostream>
using namespace std;
template <class T,class Compare>
T my_compare(T a, T b,Compare f)
{
return f(a,b);
}
template <class T>
class A
{
public:
T operator()(T a,T b)
{
return a>b?a:b;
}
};
template <class T>
class B
{
public:
T operator()(T a,T b)
{
return a>b?b:a;
}
};
int main()
{
cout<<my_compare(10,20,A<int>())<<endl;
cout<<my_compare(10,20,B<int>())<<endl;
return 0;
}
2.匿名函數(shù)對象Lambda表達(dá)式
2.1使用形式
auto + 名字 =[]()->返回值{};
具體介紹:
1.[] 中括號表示函數(shù)對象的構(gòu)造函數(shù)中是否接收外部變量。 [&] ,表示使用引用的方式獲取外部變量 [=],表示使用值的拷貝的方式獲取外部變量。
2.() 這個小括號是就函數(shù)對象中的小括號符后面的參數(shù)列表。
3.->返回值,需要就放,不需要就不放。根據(jù)自己需要,任君選擇。
4.{...} 就是函數(shù)對象的小括號運(yùn)算符的函數(shù)體。
2.2代碼實(shí)例
第一個當(dāng)[]里面沒用東西時:
#include <iostream>
using namespace std;
class A
{
public:
A()
{
}
void operator()()
{
}
};
int main()
{
auto f=[]()->void{cout<<"我是大哥"<<endl;};
f();
return 0;
}
第二個當(dāng)[]里面有東西時:(實(shí)現(xiàn)交換)
#include <iostream>
using namespace std;
class A
{
int a;
public:
A(int &a)
{
this->a=a;
}
void operator()()
{
}
};
int main()
{
int a=10;
int b=20;
auto f=[&]()->void
{
int temp=a;
a=b;
b=temp;
};
f();
cout<<a<<endl;
cout<<b<<endl;
return 0;
}
注意點(diǎn):
當(dāng)時候=號的時候,底層實(shí)現(xiàn)用的是const,這樣就不能進(jìn)行賦值等一系列操作,如下代碼,我們可以加上一個mutable,C++11所提供的新的關(guān)鍵字mutale,是一種易變是的修飾符。當(dāng)然下面這個代碼肯定不能實(shí)現(xiàn)交換,我只是用來做一個說明。
#include <iostream>
using namespace std;
class A
{
int a;
public:
A(int &a)
{
this->a=a;
}
void operator()()
{
}
};
int main()
{
int a=10;
int b=20;
auto f=[=]()mutable
{
int temp=a;
a=b;
b=temp;
};
f();
return 0;
}
3總結(jié)
在C++中Funtor也被稱為函數(shù)符:
函數(shù)符就有四種表現(xiàn)形式:
1.全局函數(shù)指針
2.成員函數(shù)指針
3.函數(shù)對象
4.Lambda匿名函數(shù)對象(Lambda表達(dá)式)
原文鏈接:https://blog.csdn.net/a2998658795/article/details/126068605
相關(guān)推薦
- 2022-03-14 Springboot中遇到的問題——Failed to load ApplicationContex
- 2022-06-14 golang連接redis庫及基本操作示例過程_Golang
- 2022-11-19 Python+random模塊實(shí)現(xiàn)隨機(jī)抽樣_python
- 2022-06-20 go程序部署到linux上運(yùn)行的實(shí)現(xiàn)方法_Golang
- 2023-02-04 python中各種路徑設(shè)置的方法詳解_python
- 2023-03-03 Fragment通過FragmentManager實(shí)現(xiàn)通信功能詳細(xì)講解_Android
- 2023-03-21 SQL數(shù)據(jù)庫的所有命令(函數(shù)、運(yùn)算符)匯總大全_數(shù)據(jù)庫其它
- 2023-02-12 Python利用物理引擎Pymunk編寫一個解壓小游戲_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支