C++函數(shù)對象Functor與匿名函數(shù)對象Lambda表達式詳解
1函數(shù)對象Functor(仿函數(shù))
1.1概念
函數(shù)對象就是類對象,生成這個類對象的類中,擁有一個小括號運算符重載函數(shù)。
重載了小括號運算符的類的類對象,就叫函數(shù)對象。
1.2代碼實例
#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)用效率要高很多
當我們用普通函數(shù)的時候:像下面這種情況,每次都需要回調(diào),特別的繁瑣。
注意:在這個代碼里面,即使我們在函數(shù)前面都加上inline變成內(nèi)聯(lián)函數(shù),也是不行的,因為我們使用的是函數(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ù)對象就可以解決這個問題,因為使用函數(shù)對象就相當于在函數(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表達式
2.1使用形式
auto + 名字 =[]()->返回值{};
具體介紹:
1.[] 中括號表示函數(shù)對象的構(gòu)造函數(shù)中是否接收外部變量。 [&] ,表示使用引用的方式獲取外部變量 [=],表示使用值的拷貝的方式獲取外部變量。
2.() 這個小括號是就函數(shù)對象中的小括號符后面的參數(shù)列表。
3.->返回值,需要就放,不需要就不放。根據(jù)自己需要,任君選擇。
4.{...} 就是函數(shù)對象的小括號運算符的函數(shù)體。
2.2代碼實例
第一個當[]里面沒用東西時:
#include <iostream>
using namespace std;
class A
{
public:
A()
{
}
void operator()()
{
}
};
int main()
{
auto f=[]()->void{cout<<"我是大哥"<<endl;};
f();
return 0;
}第二個當[]里面有東西時:(實現(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;
}注意點:
當時候=號的時候,底層實現(xiàn)用的是const,這樣就不能進行賦值等一系列操作,如下代碼,我們可以加上一個mutable,C++11所提供的新的關(guān)鍵字mutale,是一種易變是的修飾符。當然下面這個代碼肯定不能實現(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表達式)
到此這篇關(guān)于C++函數(shù)對象Functor與匿名函數(shù)對象Lambda表達式詳解的文章就介紹到這了,更多相關(guān)C++ Functor與Lambda內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中給二維指針分配內(nèi)存(實現(xiàn)代碼)
我們都知道在 C++ 中分配動態(tài)數(shù)組用的是 new , 撤銷動態(tài)數(shù)組用的是 delete[ ] ,現(xiàn)在讓我們來看看怎么利用這兩個關(guān)鍵字給二維指針分配內(nèi)存2013-10-10
C語言的isatty函數(shù)和ttyname函數(shù)以及sendmsg函數(shù)用法
這篇文章主要介紹了C語言的isatty函數(shù)和ttyname函數(shù)以及sendmsg函數(shù)用法,是C語言入門學習中的基礎知識,需要的朋友可以參考下2015-09-09
C語言中g(shù)etchar(?)?函數(shù)使用詳解
getchar()?字符輸入函數(shù),沒有參數(shù),從輸入緩沖區(qū)里面讀取一個字,需要注意一次只能讀取一個字符,這篇文章主要介紹了C語言中g(shù)etchar函數(shù)使用詳解,需要的朋友可以參考下2022-12-12

