Beruflich Dokumente
Kultur Dokumente
STLFunctionObjects(Functors)
C++ReferenceMaterial
STLFunctionObjects(Functors)
Overview
Functionobjects(alsocalledfunctors)areanSTLfeaturethatyoumaynotemployimmediately
whenyoustartusingtheSTL.Theyare,however,veryusefulinmanysituationsandanSTL
facilitywithwhichyoushouldbecomeacquainted.TheygivetheSTLaflexibilitythatitwould
nototherwisehave,andalsocontributetoSTLefficiency.Themostcommonusesforfunction
objectsareforgeneratingdata,fortestingdata,andforapplyingoperationstodata.
Afunctionobject(orfunctor)issimplyanyobjectofaclassthatprovidesatleastonedefinition
foroperator()Whatthismeansisthatifyouthendeclareanobjectfoftheclassinwhichthis
operator()isdefinedyoucansubsequentlyusethatobjectfjustlikeyouwouldusean
"ordinary"function.Forexample,youcouldhaveanassignmentstatementlike
someValue=f(arg1,arg2)
whichisthesameas
someValue=f.operator()(arg1,arg2)
solongasoperator()forthegivenclasshadbeendefinedtodothefollowingtwothings:
1. Itmusttaketwoparameters(par1andpar2,say,ofwhatevertype(s)),witharg1andarg2in
theaboveassignmentstatementbeingactualparametersofthecorrespondingtype(s).
2. ItmustreturnavalueofthetypeofthevariablesomeValue
Ifyouwereonlygoingtouseafunctorasillustratedintheassignmentstatementexampleorthe
sampleprogramgivenabove,youmightaswelluseanordinaryfunction.IntheSTL,algorithms
oftentakeaparameterwhichisafunction(orfunctor)tellingthealgorithmhowtoperformsome
partofitstask,andfunctorsaregenerallymoreversatileandhencemoreusefulforthis
purpose.Inparticular,thebuiltinfunctorsareobjectsoftemplateclasses,andsothesame
computationsperformedbyagivenfunctorcanbeappliedtodifferenttypes.
Binaryfunctors(orordinarybinaryfunctions)thatreturnabooleanvaluearecalledbinary
predicatesorcomparitorsorcomparisonfunctions.Unaryfunctors(orordinaryunaryfunctions)
thatreturnabooleanvaluearecalledunarypredicates.Eithervarietyoffunctorthatreturnsa
booleanvaluemaybereferredtosimplyasapredicatefunction.
http://cs.stmarys.ca/~porter/csc/ref/stl/function_objects.html
1/4
2015/7/6
STLFunctionObjects(Functors)
Programmerdefinedfunctorsofbinaryorunarytype
ClearlyanyC++programmermaydefineaclasswhichincludesanoverloadedoperator().
Oncethisisdone,thatC++programmerjustasclearlyhasaprogrammerdefinedfunctorathis
orherdisposal,abinaryfunctorifithastwoparametersandaunaryfunctorifithasasingle
paramter(forexample).
http://cs.stmarys.ca/~porter/csc/ref/stl/function_objects.html
2/4
2015/7/6
STLFunctionObjects(Functors)
Builtinfunctorsofbinarytype(availablefrom<functional>)
Ineachofthefunctorsofthissectionarg1andarg2bothhavetypeT.
Arithmeticbinaryfunctors
plus<T>f
f(arg1,arg2)returnsthevaluearg1+arg2.
minus<T>f
f(arg1,arg2)returnsthevaluearg1arg2.
multiplies<T>f
f(arg1,arg2)returnsthevaluearg1*arg2.
divides<T>f
f(arg1,arg2)returnsthevaluearg1/arg2.
modulus<T>f
f(arg1,arg2)returnsthevaluearg1%arg2.
Relationalbinaryfunctors
equal_to<T>f
f(arg1,arg2)returnsthevaluearg1==arg2.
not_equal_to<T>f
f(arg1,arg2)returnsthevaluearg1!=arg2.
greater<T>f
f(arg1,arg2)returnsthevaluearg1>arg2.
greater_equal<T>f
f(arg1,arg2)returnsthevaluearg1>=arg2.
less<T>f
f(arg1,arg2)returnsthevaluearg1<arg2.
less_equal<T>f
f(arg1,arg2)returnsthevaluearg1<=arg2.
Logicalbinaryfunctors
logical_and<T>f
f(arg1,arg2)returnsthevaluearg1&&arg2.
logical_or<T>f
f(arg1,arg2)returnsthevaluearg1||arg2.
http://cs.stmarys.ca/~porter/csc/ref/stl/function_objects.html
3/4
2015/7/6
STLFunctionObjects(Functors)
Builtinfunctorsofunarytype(availablefrom<functional>)
IneachofthefunctorsofthissectionarghastypeT.
Arithmeticunaryfunctor
negate<T>f
f(arg)returnsthevaluearg.
Logicalunaryfunctor
logical_not<T>f
f(arg)returnsthevalue!arg.
SamplePrograms
function_object.cpp|Windows_executable|program_output(text)
Illustratesaverysimpleprogrammerdefinedfunctionobject.
function_objects_stl.cpp|Windows_executable|program_output(text)
IllustratesseveralSTLbuiltinfunctionobjects.
http://cs.stmarys.ca/~porter/csc/ref/stl/function_objects.html
4/4