C++:函數(shù)對象,STL提供的函數(shù)對象,函數(shù)適配器詳解
1 函數(shù)對象
1.函數(shù)對象是行為類似函數(shù)的對象。一個類對象,表現(xiàn)出一個函數(shù)的特征,即通過對象名+(參數(shù)列表)的方式使用一個類對象。
2.使用STL中提供的或自定義的迭代器和**函數(shù)對象,**配合STL的算法,組合出各種各樣的功能。
3.通過函數(shù)對象而不使用函數(shù)指針,可以增加通用性,提高效率。
4.函數(shù)對象概念:泛化的函數(shù)
①將普通函數(shù)作為函數(shù)對象:傳遞函數(shù)名
#include <iostream> #include <numeric> //包含accumulate算法 #include <functional> #include <vector> using namespace std; int mult(int a, int b) { return a * b; } int main() { int a[] = { 1, 2, 3, 4, 5, 6, 7 }; const int N = sizeof(a) / sizeof(int); //用該式確定數(shù)組長度,定義為常量 cout << "所有數(shù)累乘為:" << accumulate(a, a + N, 1, mult) << endl; //將普通函數(shù)作為函數(shù)對象,傳遞函數(shù)名 //指針a,a + N也可以作為迭代器 return 0; }
②將重載了()運算符的類的對象作為函數(shù)對象:傳遞"類名()"
#include <numeric> #include <iostream> using namespace std; class MultClass { public: int operator () (int a, int b) const { return a * b; } }; int main() { int a[] = { 1, 2, 3, 4, 5, 6, 7 }; const int N = sizeof(a) / sizeof(int); //確定數(shù)組a的長度 cout << "所有數(shù)乘積為:" << accumulate(a, a + N, 1, MultClass()) << endl; //傳輸方式是類名(),輸出5040 //指針a,a + N也可以作為迭代器 MultClass ss; cout << ss(100, 100); //輸出10000 return 0; }
2 STL提供的函數(shù)對象
1.系統(tǒng)提供函數(shù)對象幫助實現(xiàn)基本功能。
2.accmulate算法接受二元函數(shù)對象,transform算法接受一元函數(shù)對象。
①STL庫的multiplies
#include <iostream> #include <functional> #include <numeric> using namespace std; int main(){ int a[] = { 1, 2, 3, 4, 5, 6, 7 }; const int N = sizeof(a) / sizeof(int); cout << accumulate(a, a + N, 1, multiplies<int>()) << endl;//通過STL自帶的函數(shù)對象multiplies實現(xiàn)乘法,注意要寫數(shù)據(jù)類型<int> //指針a,a + N也可以作為迭代器 return 0; }
②STL庫的二元謂詞greater
#include <iostream> #include <algorithm> #include <functional> //包含greater using namespace std; int main() { int arr[] = { 24, 43, 5, 4, 62, 34, 7654, 22 }; const int N = sizeof(arr) / sizeof(int); copy(arr, arr + N, ostream_iterator<int>(cout, "\t")); cout << endl; sort(arr, arr + N, greater<int>()); //包含在<algorithm>中,默認是升序 copy(arr, arr + N, ostream_iterator<int>(cout, "\t")); return 0; }
3 函數(shù)適配器
適配器顧名思義,讓函數(shù)適配算法。
Unary Predicate:一元謂詞
binary:二元的
bind:結(jié)合,(使)聯(lián)合在一起
①找出第一個大于40的數(shù),注意用數(shù)組和vector都可以
#include <iostream> #include <algorithm> #include <functional> #include <vector> using namespace std; int main() { int a[] = { 30, 40, 50, 90, 20, 10 }; const int N = sizeof(a) / sizeof(int); int *c = find_if(a, a + N, bind2nd(greater<int>(), 40)); cout << *c << endl; return 0; }
一般使用數(shù)組初始化向量vector,后續(xù)操作更方便
int main() { int a[] = { 30, 40, 50, 90, 20, 10 }; const int N = sizeof(a) / sizeof(int); vector<int> v (a, a + N); //用數(shù)組初始化vector vector<int>::iterator p = find_if (v.begin(), v.end(), bind2nd(greater<int>(), 40) ); if (p == v.end()) cout << "找不到" << endl; else cout << *p << endl; return 0; }
find_if算法在STL中的原型聲明為: template<class InputIterator, class UnaryPredicate> InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred); 它的功能是查找數(shù)組[first, last)區(qū)間中第一個pred(x)為真的元素。 InputIterator、UnaryPredicate是用概念來做模板參數(shù)名
②利用prt_fun、not1、not2產(chǎn)生組合適配器
#include <iostream> #include <functional> #include <algorithm> #include <vector> using namespace std; int g(int x, int y) { //實現(xiàn)類似greater的功能 return x > y; } int main() { int a[] = { 30, 90, 10, 23, 432, 656, 7, 78 }; const int N = sizeof(a) / sizeof(int); vector<int> v(a, a + N); auto p1 = find_if(v.begin(), v.end(), bind2nd(ptr_fun(g), 40)); //找第一個大于40的數(shù) //ptr_fun將函數(shù)指針轉(zhuǎn)換為函數(shù)對象,bind2nd將40作為二元函數(shù)對象的第二個參數(shù) if (p1 == v.end()) cout << "no element" << endl; else cout << *p1 << endl; auto p2 = find_if(v.begin(), v.end(), not1(bind2nd(ptr_fun(g), 15))); //找第一個不大于15的數(shù) //not1對一元函數(shù)對象取邏輯反,find_if找到第一個令bind2nd取false的值 if (p2 == v.end()) cout << "no element" << endl; else cout << *p2 << endl; auto p3 = find_if(v.begin(), v.end(), bind2nd(not2(ptr_fun(g)), 15)); // 找第一個不大于15的數(shù) //not2對二元函數(shù)取邏輯反 if (p3 == v.end()) cout << "no element" << endl; else cout << *p3 << endl; return 0; }
③成員函數(shù)適配器,類的成員函數(shù)要通過適配器轉(zhuǎn)換為普通函數(shù)對象
#include <iostream> #include <vector> #include <functional> #include <algorithm> using namespace std; struct Car { int id; Car(int id) { this->id = id; } void display() const { cout << "car " << id << endl; } }; int main() { vector<Car*> pcars; vector<Car> cars; for (int i = 0; i < 5; i++) pcars.push_back(new Car(i)); //push_back() 在Vector最后添加一個元素(參數(shù)為要插入的值) for (int i = 5; i < 10; i++) cars.push_back(Car(i)); cout << "elements in pcars: " << endl; for_each(pcars.begin(), pcars.end(), mem_fun(&Car::display));//for_each算法對每一個迭代器范圍中的元素進行函數(shù)對象計算 //men_fun適配后函數(shù)對象的參數(shù)為"對象的指針" cout << endl; for_each(cars.begin(), cars.end(), mem_fun_ref(&Car::display)); //men_fun_ptr適配后函數(shù)對象的參數(shù)為"對象的引用" cout << endl; for (size_t i = 0; i < pcars.size(); ++i) delete pcars[i]; return 0; }
為什么不能同全局函數(shù)一樣直接傳遞函數(shù)名而成員函數(shù)必須以 &類名::函數(shù)名 的方式,因為需要考慮static成員函數(shù)的情況。
mem_fun(member適配為function):將成員函數(shù)適配為普通函數(shù)對象,適配出來的函數(shù)需要對象的指針作為參數(shù)。
men_fun_ref:將成員函數(shù)適配為普通函數(shù)對象,適配出來的函數(shù)需要對象的引用作為參數(shù)。
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
詳解VS2019 dumpbin查看DLL的導(dǎo)出函數(shù)
這篇文章主要介紹了詳解VS2019 dumpbin查看DLL的導(dǎo)出函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08c++多線程之死鎖的發(fā)生的情況解析(包含兩個歸納,6個示例)
這篇文章主要介紹了c++多線程之死鎖的發(fā)生的情況解析(包含兩個歸納,6個示例),需要的朋友可以參考下2018-01-01深入探討C語言中局部變量與全局變量在內(nèi)存中的存放位置
本篇文章是對在C語言中局部變量與全局變量在內(nèi)存中的存放位置進行了詳細的分析介紹,需要的朋友參考下2013-05-05