STL17-函数对象
生活随笔
收集整理的這篇文章主要介紹了
STL17-函数对象
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
仿函數(shù):
#include<iostream> #include<vector> #include<algorithm> using namespace std; //仿函數(shù)(函數(shù)對(duì)象)重載“()”操作符 使類對(duì)象可以像函數(shù)那樣調(diào)用 //仿函數(shù)是一個(gè)類,不是一個(gè)函數(shù) //函數(shù)對(duì)象可以像普通函數(shù)一樣調(diào)用 //函數(shù)對(duì)象可以像普通函數(shù)那樣接收參數(shù) //函數(shù)對(duì)象超出了函數(shù)的概念,函數(shù)對(duì)象可以保存函數(shù)調(diào)用的狀態(tài)struct MyPrint { void operator()(int val) {cout << val;} }; void test01() {MyPrint print;print(10); } #if 0 int num = 0; //在開發(fā)中,避免使用全局變量 多個(gè)線程對(duì)變量處理 加鎖解鎖 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調(diào)用次數(shù):" << print.mNum << endl; //0cout << "print2調(diào)用次數(shù):" <<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() {//使用內(nèi)建函數(shù)對(duì)象聲明一個(gè)對(duì)象plus<int> myclass;cout<<myclass(10, 20)<<endl;//使用匿名臨時(shí)對(duì)象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; //仿函數(shù)適配器 bindlst bind2nd 綁定適配器 struct MyPrint {void operator()(int v) {cout << v << " ";} }; struct MyPrint2 :public binary_function<int,int,void>{ //第一個(gè)參數(shù)類型 第二個(gè)參數(shù)類型 返回值類型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;//如何給仿函數(shù)傳入兩個(gè)參數(shù)//for_each(v.begin(), v.end(), MyPrint2(100)); 錯(cuò)誤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));//綁定適配器 將一個(gè)二元函數(shù)對(duì)象轉(zhuǎn)換為一元函數(shù)對(duì)象//bindlst bind2nd 區(qū)別//bindlst將addNum綁定為函數(shù)對(duì)象的第一個(gè)參數(shù)//bind2nd將addNum綁定為函數(shù)對(duì)象的第二個(gè)參數(shù) } 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 << " ";} }; //仿函數(shù)適配器 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 區(qū)別//如果對(duì)二元謂詞取反 用 not2//如果對(duì)一元謂詞取反 用 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 << "沒(méi)有找到" << endl;}elsecout << *ret1 << endl;} void MyPrint033(int val1,int val2) {cout << val1 << " "<<val2<<" "<<endl; } void MyPrint03(int val) {cout << val << " "; } //仿函數(shù)適配器 ptr_fun 把普通函數(shù) 轉(zhuǎn)成 函數(shù)對(duì)象 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); //此處函數(shù)沒(méi)有參數(shù)cout << endl;//把普通函數(shù) 轉(zhuǎn)成 函數(shù)對(duì)象for_each(v.begin(), v.end(), bind2nd(ptr_fun(MyPrint033),10)); //此處函數(shù)沒(méi)有參數(shù) } //仿函數(shù)適配器 mem_fun mem_fun_ref class Person { public:/*Person(int age, int id) {錯(cuò)誤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() {//如果容器中存放的是對(duì)象或者對(duì)象指針,我們for_each打印時(shí),調(diào)用類//自己提供的打印函數(shù)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區(qū)別?//如果存放的是對(duì)象指針用mem_fun//如果存放的是對(duì)象,使用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總結(jié)
以上是生活随笔為你收集整理的STL17-函数对象的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: .net fileupload批量上传可
- 下一篇: 数据结构-图及其遍历