溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++中的函數(shù)對(duì)象及函數(shù)適配器的功能

發(fā)布時(shí)間:2021-08-24 09:10:43 來(lái)源:億速云 閱讀:181 作者:chen 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“C++中的函數(shù)對(duì)象及函數(shù)適配器的功能”,在日常操作中,相信很多人在C++中的函數(shù)對(duì)象及函數(shù)適配器的功能問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C++中的函數(shù)對(duì)象及函數(shù)適配器的功能”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

目錄
  • 1 函數(shù)對(duì)象

  • 2 STL提供的函數(shù)對(duì)象

  • 3 函數(shù)適配器

  • 總結(jié)

1 函數(shù)對(duì)象

1.函數(shù)對(duì)象是行為類似函數(shù)的對(duì)象。一個(gè)類對(duì)象,表現(xiàn)出一個(gè)函數(shù)的特征,即通過(guò)對(duì)象名+(參數(shù)列表)的方式使用一個(gè)類對(duì)象。

2.使用STL中提供的或自定義的迭代器和**函數(shù)對(duì)象,**配合STL的算法,組合出各種各樣的功能。

3.通過(guò)函數(shù)對(duì)象而不使用函數(shù)指針,可以增加通用性,提高效率。

4.函數(shù)對(duì)象概念:泛化的函數(shù)

C++中的函數(shù)對(duì)象及函數(shù)適配器的功能

①將普通函數(shù)作為函數(shù)對(duì)象:傳遞函數(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ù)組長(zhǎng)度,定義為常量
    cout << "所有數(shù)累乘為:" << accumulate(a, a + N, 1, mult) << endl; //將普通函數(shù)作為函數(shù)對(duì)象,傳遞函數(shù)名
    //指針a,a + N也可以作為迭代器
    return 0;
}

②將重載了()運(yùn)算符的類的對(duì)象作為函數(shù)對(duì)象:傳遞"類名()"

#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的長(zhǎng)度
    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ù)對(duì)象

C++中的函數(shù)對(duì)象及函數(shù)適配器的功能

1.系統(tǒng)提供函數(shù)對(duì)象幫助實(shí)現(xiàn)基本功能。

2.accmulate算法接受二元函數(shù)對(duì)象,transform算法接受一元函數(shù)對(duì)象。

①STL庫(kù)的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;//通過(guò)STL自帶的函數(shù)對(duì)象multiplies實(shí)現(xiàn)乘法,注意要寫(xiě)數(shù)據(jù)類型<int>
	//指針a,a + N也可以作為迭代器
	return 0;
}

②STL庫(kù)的二元謂詞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>中,默認(rèn)是升序
	copy(arr, arr + N, ostream_iterator<int>(cout, "\t"));
	return 0;
}

3 函數(shù)適配器

適配器顧名思義,讓函數(shù)適配算法。

Unary Predicate:一元謂詞

binary:二元的

bind:結(jié)合,(使)聯(lián)合在一起

C++中的函數(shù)對(duì)象及函數(shù)適配器的功能

C++中的函數(shù)對(duì)象及函數(shù)適配器的功能

①找出第一個(gè)大于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ū)間中第一個(gè)pred(x)為真的元素。
InputIterator、UnaryPredicate是用概念來(lái)做模板參數(shù)名

②利用prt_fun、not1、not2產(chǎn)生組合適配器

#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;
int g(int x, int y) { //實(shí)現(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)); //找第一個(gè)大于40的數(shù)
    //ptr_fun將函數(shù)指針轉(zhuǎn)換為函數(shù)對(duì)象,bind2nd將40作為二元函數(shù)對(duì)象的第二個(gè)參數(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))); //找第一個(gè)不大于15的數(shù)
    //not1對(duì)一元函數(shù)對(duì)象取邏輯反,find_if找到第一個(gè)令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)); // 找第一個(gè)不大于15的數(shù)
    //not2對(duì)二元函數(shù)取邏輯反
    if (p3 == v.end())
        cout << "no element" << endl;
    else
        cout << *p3 << endl;
    return 0;
}

③成員函數(shù)適配器,類的成員函數(shù)要通過(guò)適配器轉(zhuǎn)換為普通函數(shù)對(duì)象

#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最后添加一個(gè)元素(參數(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算法對(duì)每一個(gè)迭代器范圍中的元素進(jìn)行函數(shù)對(duì)象計(jì)算
	//men_fun適配后函數(shù)對(duì)象的參數(shù)為"對(duì)象的指針"
	cout << endl;
	for_each(cars.begin(), cars.end(), mem_fun_ref(&Car::display));
	//men_fun_ptr適配后函數(shù)對(duì)象的參數(shù)為"對(duì)象的引用"
	cout << endl;
	for (size_t i = 0; i < pcars.size(); ++i)
		delete pcars[i];
	return 0;
}

為什么不能同全局函數(shù)一樣直接傳遞函數(shù)名而成員函數(shù)必須以 &類名::函數(shù)名 的方式,因?yàn)樾枰紤]static成員函數(shù)的情況。
mem_fun(member適配為function):將成員函數(shù)適配為普通函數(shù)對(duì)象,適配出來(lái)的函數(shù)需要對(duì)象的指針作為參數(shù)。
men_fun_ref:將成員函數(shù)適配為普通函數(shù)對(duì)象,適配出來(lái)的函數(shù)需要對(duì)象的引用作為參數(shù)。

到此,關(guān)于“C++中的函數(shù)對(duì)象及函數(shù)適配器的功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI