1.函數(shù)對(duì)象
-
函數(shù)對(duì)象(仿函數(shù)):
重載函數(shù)調(diào)用操作的類(lèi),其對(duì)象常稱(chēng)之為函數(shù)對(duì)象;
函數(shù)對(duì)象使用重載()時(shí),其行為類(lèi)似函數(shù)調(diào)用,也叫仿函數(shù); - 函數(shù)對(duì)象本質(zhì):
函數(shù)對(duì)象(仿函數(shù))本質(zhì)是一個(gè)類(lèi),不是一個(gè)函數(shù)。
- 函數(shù)對(duì)象特點(diǎn):
函數(shù)對(duì)象在使用時(shí)可以有形參、有返回值。
函數(shù)對(duì)象可以有自己的狀態(tài)值。
函數(shù)對(duì)象可以作為函數(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)建一個(gè)函數(shù)對(duì)象
myfunc p1;
cout < "t函數(shù)對(duì)象形參返回使用示例:" < 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");
}

2.謂詞
-
謂詞:
函數(shù)對(duì)象返回值為bool類(lèi)型,則稱(chēng)之為謂詞; -
一元謂詞:
仿函數(shù)的形參只有一個(gè); -
二元謂詞:
仿函數(shù)的形參有兩個(gè)參數(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ù)對(duì)象
if (ret ==vtr.end())
{
cout < "未查到到?>5的值!" < endl;
}
else
{
cout < "查找成功,大于5的值為:" <*ret

3.內(nèi)建函數(shù)對(duì)象
-
內(nèi)建函數(shù)對(duì)象:
STL中提供了一些內(nèi)建函數(shù)對(duì)象:算術(shù)仿函數(shù)、關(guān)系仿函數(shù)、邏輯仿函數(shù) --頭文件
3.1算術(shù)運(yùn)算符
- 算術(shù)仿函數(shù):實(shí)現(xiàn)四則運(yù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是一元運(yùn)算(只有一個(gè)參數(shù)),其余均為二元運(yùn)算。
#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減法取絕對(duì)值:" < 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)系運(yùn)算符
- 內(nèi)建仿函數(shù):關(guān)系運(yù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;
/*
要實(shí)現(xiàn)從大小,可以自行實(shí)現(xiàn)一個(gè)仿函數(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");
}

3.3邏輯運(yùn)算符
- 內(nèi)建仿函數(shù)--邏輯運(yùn)算符
邏輯與: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");
}

審核編輯:湯梓紅
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4379瀏覽量
64820 -
C++
+關(guān)注
關(guān)注
22文章
2119瀏覽量
75250
發(fā)布評(píng)論請(qǐng)先 登錄
GCC內(nèi)建函數(shù)問(wèn)題?。。?/a>
C++教程之函數(shù)的遞歸調(diào)用
C++課程資料詳細(xì)資料合集包括了:面向對(duì)象程序設(shè)計(jì)與C++,算法,函數(shù)等

如何在中斷C函數(shù)中調(diào)用C++

C++之拷貝構(gòu)造函數(shù)的淺copy及深copy
C++之函數(shù)模板的概念及意義
C++之重載函數(shù)學(xué)習(xí)總結(jié)
C++基礎(chǔ)語(yǔ)法之inline 內(nèi)聯(lián)函數(shù)
在C++中如何用虛函數(shù)實(shí)現(xiàn)多態(tài)
如何在MPLAB XC16編譯器內(nèi)建函數(shù)

虛函數(shù),C++開(kāi)發(fā)者如何有效利用
深度解析C++中的虛函數(shù)

評(píng)論