生活随笔
收集整理的這篇文章主要介紹了
STL之函数对象
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為了使類屬算法具有靈活性,STL常使用函數的重載機制為算法提供兩種形式。算法的第一種形式使用的是常規的操作來實現。第二種形式中,算法可以根據用戶指定的準測對元素經行處理。
函數對象包含了一個可以通過函數調用運算符()使用的函數。實際上,函數對象是重載了函數調用運算符operator()的類模板。
用戶可以創建自己的函數對象。STL提供了算術函數對象,關系函數對象,邏輯函數對象。
算術函數對象:
plus<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 加minus<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?減multiplies<type> ? ? ? ? ? ? ? ? ? ? ? 乘divides<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ?除modulus<type> ? ? ? ? ? ? ? ? ? ? ? ?模negate<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ?取反
示例代碼:
#include?<iostream>?? #include?<algorithm>?? #include?<string>?? #include?<numeric>?? #include?<iterator>?? #include?<vector>?? #include?<functional>?? ?? using?namespace?std;?? ?? int?functAdd(plus<int>,int,int);?? ?? int?main()?{?? ?????? ????plus<int>?addNum;?? ????int?sum?=?addNum(34,56);?? ????cout?<<?"Sum="?<<?sum?<<?endl;?? ?? ?????? ????plus<string>?joinString;?? ????string?s1?=?"hello";?? ????string?s2?=?"There";?? ?? ????string?str?=?joinString(s1,s2);?? ????cout?<<?"str="?<<?str?<<?endl;?? ?? ?????? ????cout?<<?"funcAdd="?<<?functAdd(addNum,34,26)?<<?endl;?? ?? ????int?list[8]?=?{1,2,3,4,5,6,7,8};?? ????vector<int>?intsList(list,list+8);?? ????ostream_iterator<int>?screenOut(cout,?"?");?? ?? ????cout?<<?"intList:";?? ????copy(intsList.begin(),intsList.end(),screenOut);?? ????cout?<<?endl;?? ?? ?????? ????int?suma?=?accumulate(intsList.begin(),intsList.end(),0);?? ????cout?<<?"accumulate:"?<<?suma?<<?endl;?? ?? ????int?product?=?accumulate(intsList.begin(),intsList.end(),1,multiplies<int>());?? ?? ????cout?<<?"product:"?<<?product?<<?endl;?? ?? ????return?0;?? }?? ?? int?functAdd(plus<int>?sum,int?x,int?y)?{?? ????return?sum(x,?y);?? }??
運行結果:
intQueue.front:26
intQueue.back:33
intQueue.front:18
intQueue :
18 50 33
關系函數對象:
equal_to<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?等于not_equal_to<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?不等于greater<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 大于greater_equal<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? 大于等于less<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?小于less_equal<type> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 小于等于
示例代碼:
#include?<iostream>?? #include?<algorithm>?? #include?<string>?? #include?<numeric>?? #include?<iterator>?? #include?<vector>?? #include?<functional>?? ?? using?namespace?std;?? ?? int?main()?{?? ?????? ????equal_to<int>?compare;?? ????bool?isEqual?=?compare(6,6);?? ????cout?<<?"isEqual="?<<?isEqual?<<?endl;?? ?? ?????? ????greater<string>?greaterString;?? ????string?s1?=?"hello";?? ????string?s2?=?"there";?? ?? ????if?(greaterString(s1,?s2))?? ????{?? ????????cout?<<?s1?<<?"?is?greater?than?"?<<?s2?<<?endl;?? ????}?else?{?? ????????cout?<<?s2?<<?"?is?greater?than?"?<<?s1?<<?endl;?? ????}?? ?? ????int?temp[8]?=?{2,3,4,5,1,7,8,9};?? ????vector<int>?vecList(temp,temp+8);?? ????vector<int>::iterator?intItr1,intItr2;?? ????ostream_iterator<int>?screen(cout,?"?");?? ????cout?<<?"vecList:"?<<endl;?? ????copy(vecList.begin(),vecList.end(),screen);?? ????cout?<<?endl;?? ?? ????intItr1?=?adjacent_find(vecList.begin(),vecList.end(),greater<int>());?? ????intItr2?=?intItr1?+?1;?? ?? ????cout?<<?"intItr1:"?<<?*intItr1?<<",intItr2:"?<<?*intItr2?<<?endl;?? ????cout?<<?"psition:"?<<?vecList.end()?-?intItr2?<<?endl;?? ????cout?<<?"psition:"?<<?intItr2?-?vecList.begin()?<<?endl;?? ?? ????return?0;?? }??
運行結果:
isEqual=1
there is greater than hello
vecList:
2 3 4 5 1 7 8 9
intItr1:5,intItr2:1
psition:4
psition:4
邏輯運算對象:
logical_not<type>logical_and<type>logical_or<type>
總結
以上是生活随笔為你收集整理的STL之函数对象的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。