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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL17-函数对象

發布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL17-函数对象 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

仿函數:

#include<iostream> #include<vector> #include<algorithm> using namespace std; //仿函數(函數對象)重載“()”操作符 使類對象可以像函數那樣調用 //仿函數是一個類,不是一個函數 //函數對象可以像普通函數一樣調用 //函數對象可以像普通函數那樣接收參數 //函數對象超出了函數的概念,函數對象可以保存函數調用的狀態struct MyPrint { void operator()(int val) {cout << val;} }; void test01() {MyPrint print;print(10); } #if 0 int num = 0; //在開發中,避免使用全局變量 多個線程對變量處理 加鎖解鎖 void MyPrint02(int val) {cout << val;num++; } #endif //避免全局變量的方法 struct MyPrint02 {MyPrint02(){mNum = 0;}void operator()(int val) {mNum++;cout << val<<endl;} public:int mNum; };void test02() {MyPrint02 print;print(10);print(20);cout << print.mNum << endl; } void test03() {vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);MyPrint02 print;MyPrint02 print2=for_each(v.begin(), v.end(), print);cout << "print調用次數:" << print.mNum << endl; //0cout << "print2調用次數:" <<print2.mNum << endl; //4} #if 1 int main() {cout << "test01" << endl;test01();cout << endl<<"test02" << endl;test02();cout <<endl<< "test03" << endl;test03();return 0; } #endif

?

#include<iostream> #include<functional> using namespace std;void test01() {//使用內建函數對象聲明一個對象plus<int> myclass;cout<<myclass(10, 20)<<endl;//使用匿名臨時對象cout << plus<int>()(5, 6) << endl; } #if 1 int main() {test01() ;return 0; } #endif

?

#include<iostream> #include<vector> #include<functional> #include<algorithm> using namespace std; //仿函數適配器 bindlst bind2nd 綁定適配器 struct MyPrint {void operator()(int v) {cout << v << " ";} }; struct MyPrint2 :public binary_function<int,int,void>{ //第一個參數類型 第二個參數類型 返回值類型void operator()(int v,int value) const{cout << "v:" << v <<" " <<"val:" << value << endl;//cout << v+value << " ";} }; void test01() {vector<int> v;for (int i = 0; i < 10; i++) {v.push_back(i);}MyPrint print;for_each(v.begin(), v.end(), print);cout << endl;for_each(v.begin(), v.end(), MyPrint());cout << endl;//如何給仿函數傳入兩個參數//for_each(v.begin(), v.end(), MyPrint2(100)); 錯誤int addNum = 100;cout << "------------bind2nd-----------------" << endl;for_each(v.begin(), v.end(), bind2nd(MyPrint2(),addNum));cout << "------------bind1st-----------------" << endl;for_each(v.begin(), v.end(), bind1st(MyPrint2(), addNum));//綁定適配器 將一個二元函數對象轉換為一元函數對象//bindlst bind2nd 區別//bindlst將addNum綁定為函數對象的第一個參數//bind2nd將addNum綁定為函數對象的第二個參數 } struct MyCompare01 {bool operator()(int v1, int v2){return v1 > v2;} }; struct MyCompare02 :public binary_function<int,int,bool>{bool operator()(int v1, int v2) const{return v1 > v2;} }; struct MyGreater5 {bool operator()(int v) {return v > 5;} }; struct MyGreater55:public unary_function<int,bool> {bool operator()(int v) const {return v > 5;} }; struct MyPrint02 {void operator()(int v) {cout << v << " ";} }; //仿函數適配器 not1 not2 取反適配器 void test02() {vector<int> v;for (int i = 0; i < 10; i++) {/*v.push_back(rand()%100+10);*/v.push_back(i);}for_each(v.begin(), v.end(), MyPrint02());cout <<endl<< "-------排序后-------"<<endl;sort(v.begin(), v.end(), MyCompare01());for_each(v.begin(), v.end(), MyPrint02()); cout<<endl << "-------取反排序后-------" << endl;sort(v.begin(), v.end(), not2(MyCompare02()));for_each(v.begin(), v.end(), MyPrint02());//not1 和 not2 區別//如果對二元謂詞取反 用 not2//如果對一元謂詞取反 用 not1cout <<endl<< "not1" << endl;vector<int>::iterator ret=find_if(v.begin(), v.end(), MyGreater5());cout << *ret << endl;vector<int>::iterator ret1 = find_if(v.begin(), v.end(),not1( MyGreater55()));if (ret1 == v.end()) {cout << "沒有找到" << endl;}elsecout << *ret1 << endl;} void MyPrint033(int val1,int val2) {cout << val1 << " "<<val2<<" "<<endl; } void MyPrint03(int val) {cout << val << " "; } //仿函數適配器 ptr_fun 把普通函數 轉成 函數對象 void test03() {vector<int> v;for (int i = 0; i < 10; i++) {/*v.push_back(rand()%100+10);*/v.push_back(i);}for_each(v.begin(), v.end(), MyPrint03); //此處函數沒有參數cout << endl;//把普通函數 轉成 函數對象for_each(v.begin(), v.end(), bind2nd(ptr_fun(MyPrint033),10)); //此處函數沒有參數 } //仿函數適配器 mem_fun mem_fun_ref class Person { public:/*Person(int age, int id) {錯誤age = age;id = id;}*/ /*Person(int age, int id) { 正確this->age = age;this->id = id;}*/Person(int age, int id) :age(age), id(id) {} //正確void show() {cout << "age:" << age << "id:" << id << endl;} public:int age;int id; }; struct PrintPerson {void operator()(Person p) {p.show();} }; void test04() {//如果容器中存放的是對象或者對象指針,我們for_each打印時,調用類//自己提供的打印函數vector<Person> vp;Person p1(10, 20);Person p2(20, 30);Person p3(30, 40);Person p4(40, 50);vp.push_back(p1);vp.push_back(p2);vp.push_back(p3);vp.push_back(p4);for_each(vp.begin(), vp.end(), PrintPerson());cout << "-----mem_fun_ref使用--------" << endl;for_each(vp.begin(), vp.end(), mem_fun_ref(&Person::show));vector<Person*> vpp;vpp.push_back(&p1);vpp.push_back(&p2);vpp.push_back(&p3);vpp.push_back(&p4);cout << "-----mem_fun使用--------" << endl;for_each(vpp.begin(), vpp.end(), mem_fun(&Person::show));//mem_fun_ref mem_fun區別?//如果存放的是對象指針用mem_fun//如果存放的是對象,使用mem_fun_ref} #if 1 int main() {cout << "test01" << endl;test01();cout << endl<<"test02" << endl;test02();cout <<endl<< "test03" << endl;test03();cout << endl << "test04" << endl;test04(); } #endif

總結

以上是生活随笔為你收集整理的STL17-函数对象的全部內容,希望文章能夠幫你解決所遇到的問題。

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