1.函數(shù)對象
-
函數(shù)對象(仿函數(shù)):
重載函數(shù)調(diào)用操作的類,其對象常稱之為函數(shù)對象;
函數(shù)對象使用重載()時,其行為類似函數(shù)調(diào)用,也叫仿函數(shù); - 函數(shù)對象本質(zhì):
函數(shù)對象(仿函數(shù))本質(zhì)是一個類,不是一個函數(shù)。
- 函數(shù)對象特點:
函數(shù)對象在使用時可以有形參、有返回值。
函數(shù)對象可以有自己的狀態(tài)值。
函數(shù)對象可以作為函數(shù)形參。
使用示例:
#include
using namespace std;
class myfunc
{
public:
myfunc()
{
count = 0;
}
//求和示例,重載()
int operator()(int a, int b)
{
return a + b;
}
//輸出示例,count記錄函數(shù)調(diào)用次數(shù)
void operator()(string str)
{
count++;
cout < str < endl;
}
int count;
};
void print(myfunc& p, string test)
{
p(test);
}
void test()
{
//創(chuàng)建一個函數(shù)對象
myfunc p1;
cout < "t函數(shù)對象形參返回使用示例:" < endl;
int ret=p1(10, 20);
cout < "ret=" < ret < endl;
cout < "t仿函數(shù)重載示例:" < endl;
p1("C++學(xué)習(xí)--仿函數(shù)使用示例!");
p1("C++學(xué)習(xí)--仿函數(shù)使用示例!");
p1("C++學(xué)習(xí)--仿函數(shù)使用示例!");
cout < "函數(shù)調(diào)用次數(shù):" < p1.count < endl;
cout < "t仿函數(shù)作為函數(shù)形參:" < endl;
print(p1, "hello,歡迎學(xué)習(xí)c++課程");
}
int main()
{
test();
system("pause");
}
![wKgaomSzow6AR7vbAAV4tKPvhz8386.png](https://file1.elecfans.com/web2/M00/8C/DA/wKgaomSzow6AR7vbAAV4tKPvhz8386.png)
2.謂詞
-
謂詞:
函數(shù)對象返回值為bool類型,則稱之為謂詞; -
一元謂詞:
仿函數(shù)的形參只有一個; -
二元謂詞:
仿函數(shù)的形參有兩個參數(shù);
#include
#include
#include
using namespace std;
class Check
{
public:
bool operator()(int val)
{
return val > 5;
}
bool operator()(int a1,int a2)
{
return a1 > a2;
}
};
void test()
{
vectorvtr;
/*插入數(shù)據(jù)*/
for (int i = 0; i < 10; i++)
{
vtr.push_back(i);
}
cout < "一元謂詞示例:查找vector容器中?>5的值" < endl;
/*查找vector容器中?>5的值*/
vector::iterator ret=find_if(vtr.begin(), vtr.end(), Check());//Check() ---匿名函數(shù)對象
if (ret ==vtr.end())
{
cout < "未查到到?>5的值!" < endl;
}
else
{
cout < "查找成功,大于5的值為:" <*ret
![wKgaomSzo36AfA7AAARZcc40veA240.png](https://file1.elecfans.com/web2/M00/8C/DA/wKgaomSzo36AfA7AAARZcc40veA240.png)
3.內(nèi)建函數(shù)對象
-
內(nèi)建函數(shù)對象:
STL中提供了一些內(nèi)建函數(shù)對象:算術(shù)仿函數(shù)、關(guān)系仿函數(shù)、邏輯仿函數(shù) --頭文件
3.1算術(shù)運算符
- 算術(shù)仿函數(shù):實現(xiàn)四則運算。
加法:template T plus
減法:template T minus
乘法:template T mutiplies
除法:template T divides
取模:template T modulus
取反:template T negate --正數(shù)變負(fù)數(shù),負(fù)數(shù)變正數(shù)
注意:其中negate是一元運算(只有一個參數(shù)),其余均為二元運算。
#include
using namespace std;
#include
void test()
{
//negate使用示例:
negate n;
cout < "negate取反示例:" < n(188) < endl;
plus p;
cout < "plus加法:" < p(10, 20) < endl;
minusm;
cout < "minus減法取絕對值:" < n(m(10, 20)) < endl;
multipliesmt;
cout < "multiplies乘法:" < mt(5, 3.15) < endl;
dividesd;
cout < "divides除法:" < d(10, 3) < endl;
modulusmd;
cout < "modulus取模:" < md(10, 3) < endl;
}
int main()
{
test();
system("pause");
}
3.2關(guān)系運算符
- 內(nèi)建仿函數(shù):關(guān)系運算符
大于: templatebool greater
大于等于:templatebool greater_equal
小于: templatebool less
小于等于:templatebool less_equal
等于: templatebool equal_to
不等于: templatebool not_equal_to
#include
using namespace std;
#include
#include
#include
void print(int val)
{
cout < val < " ";
}
int main()
{
vector vtr;
vtr.push_back(10);
vtr.push_back(40);
vtr.push_back(30);
vtr.push_back(60);
vtr.push_back(6);
/*sort排序,默認(rèn)是從小到大,其默認(rèn)的仿函數(shù)即less*/
sort(vtr.begin(), vtr.end());
for_each(vtr.begin(), vtr.end(), print);
cout < endl;
/*
要實現(xiàn)從大小,可以自行實現(xiàn)一個仿函數(shù)
class mycompare
{
public:
bool operator()(int a1,int a2)
{
return a1?>a2;
}
}
也可以直接使用STL內(nèi)建仿函數(shù):greater()
*/
sort(vtr.begin(), vtr.end(), greater());
for_each(vtr.begin(), vtr.end(), print);
cout < endl;
system("pause");
}
![wKgZomSzpV-AOdCcAARJXftbJKg672.png](https://file1.elecfans.com/web2/M00/8C/D9/wKgZomSzpV-AOdCcAARJXftbJKg672.png)
3.3邏輯運算符
- 內(nèi)建仿函數(shù)--邏輯運算符
邏輯與:templatebool logical_and
邏輯或: templatebool logical_or
邏輯非: templatebool logical_not
#include
using namespace std;
#include
#include
#include
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());//設(shè)置vtr2的容器大小
//將vtr容器內(nèi)容取非放到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");
}
![wKgaomSzpa-AF8o4AAPxhLsIPiA710.png](https://file1.elecfans.com/web2/M00/8C/DA/wKgaomSzpa-AF8o4AAPxhLsIPiA710.png)
審核編輯:湯梓紅
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4346瀏覽量
62999 -
C++
+關(guān)注
關(guān)注
22文章
2114瀏覽量
73878
發(fā)布評論請先 登錄
相關(guān)推薦
GCC內(nèi)建函數(shù)問題?。?!
C++教程之函數(shù)的遞歸調(diào)用
C++課程資料詳細(xì)資料合集包括了:面向對象程序設(shè)計與C++,算法,函數(shù)等
![<b class='flag-5'>C++</b>課程資料詳細(xì)資料合集包括了:面向<b class='flag-5'>對象</b>程序設(shè)計與<b class='flag-5'>C++</b>,算法,<b class='flag-5'>函數(shù)</b>等](https://file.elecfans.com/web1/M00/56/EE/pIYBAFtDKEWAY420AAB8HnKodUs131.png)
如何在中斷C函數(shù)中調(diào)用C++
![如何在中斷<b class='flag-5'>C</b><b class='flag-5'>函數(shù)</b>中調(diào)用<b class='flag-5'>C++</b>](https://file.elecfans.com/web1/M00/91/BA/pIYBAFzTzJuAT0pBAAEyN2vFGuQ683.png)
C++之拷貝構(gòu)造函數(shù)的淺copy及深copy
C++之函數(shù)模板的概念及意義
C++之重載函數(shù)學(xué)習(xí)總結(jié)
C++基礎(chǔ)語法之inline 內(nèi)聯(lián)函數(shù)
在C++中如何用虛函數(shù)實現(xiàn)多態(tài)
如何在MPLAB XC16編譯器內(nèi)建函數(shù)
![如何在MPLAB XC16編譯器<b class='flag-5'>內(nèi)建函數(shù)</b>](https://file.elecfans.com//web2/M00/89/E8/pYYBAGO5PWWAA4EdAAEUB2TN8to519.jpg)
虛函數(shù),C++開發(fā)者如何有效利用
深度解析C++中的虛函數(shù)
![深度解析<b class='flag-5'>C++</b>中的虛<b class='flag-5'>函數(shù)</b>](https://file.elecfans.com/web2/M00/91/66/pYYBAGPsTWqAcRlFAAEnF_VliNI953.jpg)
評論