日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++中的函数适配器

發布時間:2023/11/30 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++中的函数适配器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

函數適配器

函數適配器概念

STL中定義了大量的函數對象,但是有時候需要對函數返回值進行進一步的簡單計算,或者填上多余的參數,不能直接代入算法,函數適配器實現了這一功能,將一種函數對象轉化為另一種符合要求的函數對象,函數適配器可以分為4大類,綁定適配器,組合適配器,指針函數適配器和成員函數適配器

直接構造STL中的函數適配器通常會導致冗長的類型聲明。為簡化安徽念書適配器的構造,STL還提供了函數適配器輔助函數,借助于泛型自動推斷技術,無須顯式的類型聲明便可實現函數適配器的構造

常用函數函數適配器

標準庫提供一組函數適配器,用來特殊化或者擴展一元和二元函數對象。常用適配器是: 1 綁定器(binder):binder 通過把二元函數對象的一個實參綁定到一個特殊的值上,將其轉 換成一元函數對象。C++標準庫提供兩種預定義的 binder 適配器:bind1st 和 bind2nd,前 者把值綁定到二元函數對象的第一個實參上,后者綁定在第二個實參上。 2 取反器(negator):negator 是一個將函數對象的值翻轉的函數適配器。標準庫提供兩個預定 義的 ngeator 適配器:not1 翻轉一元預定義函數對象的真值,而 not2 翻轉二元謂詞函數的真 值。
常用函數適配器列表如下

  • bind1st(op,value)

  • bind2nd(op,value)

  • not1(op)

  • not2(op)

  • mem_fun_ref(op)

  • mem_fun(op) ptr_fun(op)

    #include<iostream>using namespace std; #include<vector> #include<algorithm> #include<functional>class MyPrint:public binary_function<int,int,void> { public:void operator()(int v, int start) const{cout << "v=" << v << "start=" << start << "v+start=" << v + start << endl;} };void test01() {vector<int>v;for (int i = 0; i < 10; i++) {v.push_back(i);}cout << "請輸入一個起始值:" << endl;int num;cin >> num;//for_each(v.begin(), v.end(), bind2nd (MyPrint(),num));for_each(v.begin(), v.end(), bind1st(MyPrint(), num));} //第一步,綁定數據 bind2nd //第二步,繼承類 binary_function<參數類型1,參數類型2,返回值類型> //第三步,加const修飾operator()int main() {test01();system("pause");return 0; }
  • 取反適配器

    class MyPrint:public binary_function<int,int,void> { public:void operator()(int v, int start) const{cout << "v=" << v << "start=" << start << "v+start=" << v + start << endl;} };//取反適配器 class CreateThenFive:public unary_function<int,bool> { public:bool operator()(int v)const{return v > 5;} };void test02(){//一元取反vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找大于5的數字//需求改為找小于5的數字//vector<int>::iterator pos=find_if(v.begin(), v.end(),not1 (CreateThenFive()));vector<int>::iterator pos=find_if(v.begin(), v.end(),not1(bind2nd(greater<int>(),5)));if (pos != v.end()){cout << "找到大于5的數字為:" <<*pos<< endl;}else{cout << "未找到" << endl;} }//一元取反適配器 not1 //繼承unary_fuction<類型1,返回值類型> //const

    函數指針適配器

    void MyPrint03(int v,int start){cout << v+start << endl;}//函數指針適配器void test03(){vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//將函數指針 適配為函數對象//ptr_funfor_each(v.begin(), v.end(),bind2nd (ptr_fun (MyPrint03),100));}

    成員函數適配器

    //成員函數適配器 class Person { public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}void showPerson(){cout << "成員函數中姓名:" << m_Name << "年齡:" << m_Age << endl;}void plusAge(){this->m_Age+=100;}string m_Name;int m_Age; };void MyPrintPerson(Person &p) {cout << "姓名:" << p.m_Name << "年齡:" << p.m_Age << endl; } void test04() {vector<Person >v;Person p1("aaa", 10);Person p2("bbb", 15);Person p3("ccc", 18);Person p4("ddd", 40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//成員函數適配器//mem_fun_reffor_each(v.begin(), v.end(),mem_fun_ref (&Person::showPerson));for_each(v.begin(), v.end(), mem_fun_ref(&Person::plusAge));for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson)); }

    總結

    以上是生活随笔為你收集整理的c++中的函数适配器的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。