greater_equal
greaterthanorequalcomparisonx>=y
less_equal
lessthanorequalcomparisonx<=y
Logicalfunctions
logical_and
logicalconjunctionx&&y
logical_or
logicaldisjunctionx||y
logical_not
logicalnegation!
x
Let'slookatacoupleofexamplesthatshowhowthesemightbeused.Thefirstexampleusesstd:
:
plus()tocomputetheby-elementadditionoftwolistsofintegervalues,placingtheresultbackintothefirstlist.Thiscanbeperformedbythefollowingcode:
std:
:
transform(listOne.begin(),listOne.end(),listTwo.begin(),
listOne.begin(),std:
:
plus());
Thesecondexamplenegateseveryelementinavectorofbooleanvalues:
std:
:
transform(aVec.begin(),aVec.end(),aVec.begin(),
std:
:
logical_not());
ThebaseclasstemplatesusedbytheC++StandardLibrarytodefinethefunctionsinTable 5arealsoavailableforcreatingnewunaryandbinaryfunctionobjects.Theclasstemplatesunary_functionandbinary_functionaredefinedintheheader.
Thebaseclassesaredefinedasfollows:
namespacestd{
template
structunary_function
{
typedefArgargument_type;
typedefResultresult_type;
};
template
structbinary_function
{
typedefArg1first_argument_type;
typedefArg2second_argument_type;
typedefResultresult_type;
};
}
AnexampleoftheuseofthesetemplatesisfoundinSection 6.3.TherewewanttotakeabinaryfunctionoftypeWidgetandanargumentoftypeint,andcomparethewidgetidentificationnumberagainsttheintegervalue.Afunctiontodothisiswritteninthefollowingmanner:
structWidgetTester:
std:
:
binary_function{
booloperator()(constWidget&wid,inttestid)const
{returnwid.id==testid;}
};
Theimportanceofinheritancefromoneofthetwobaseclasstemplatesbecomesapparentwhenelementaryfunctionobjectsarecomposedtoformcomplexexpressionsusingnegatorsandbinders,whichareexplainedinSection 3.5.
3.2.2.2ToImproveExecution
Asecondreasontoconsiderusingfunctionobjectsinsteadoffunctionsisfastercode.Thedifferencebetweenafunctionandanobjectofaclassthatdefinesthememberoperator()()isthatfunctionsareconvertedtopointerswhenpassedasargumentstootherfunctions,thusincurringtheoverheadofafunctioncalleveniftheyaredeclaredinline.Inmanycasesaninvocationofafunctionobject,asintheexamplesonstd:
:
transform()inSection 3.2.2.1,canbeexpandedin-line,eliminatingtheoverheadofafunctioncall.
3.2.2.3ToAccessorSetStateInformation
Thethirdmajorreasontouseafunctionobjectinplaceofafunctioniswheneachinvocationofthefunctionmustremembersomestatesetbyearlierinvocations.Anexampleofthisoccursinthecreationofagenerator,tobeusedwiththegenericalgorithmstd:
:
generate().Ageneratorissimplyafunctionobjectthattakesnoargumentsandreturnsanew(andpossiblydifferent)valueeachtimeitisinvoked.Themostcommonlyusedformofgeneratorisarandomnumbergenerator,butthereareotherusesfortheconcept.Asequencegeneratorsimplyreturnsthevaluesofanincreasingsequenceofnaturalnumbers(1,2,3,4andsoon).WecancallthisobjectiotaGenafterthesimilaroperationintheprogramminglanguageAPL,anddefineitasfollows:
classiotaGen
{
public:
iotaGen(intstart=0):
current(start){}
intoperator()(){returncurrent++;}
private:
intcurrent;
};
Aniotaobjectmaintainsacurrentvalue,whichcanbesetbytheconstructor,ordefaultstozero.Eachtimethefunction-calloperatorisinvoked,thecurrentvalueisreturned,andalsoincremented.Usingthisobject,thefollowingcallontheC++StandardLibraryfunctionstd:
:
generate()initializesavectorof20elementswiththevalues1through20:
std:
:
vectoraVec(20);
std:
:
generate(aVec.begin(),aVec.end(),iotaGen
(1));
Amorecomplexexampleofusingafunctionobjectoccursintheradixsortingexampleprogram,whichisgivenasanexampleofusingthelistdatatypeinSection 6.3.Inthisprogramreferencesareinitializedinthefunctionobject,sothatduringthesequenceofinvocationsthefunctionobjectcanaccessandmodifylocalvaluesinthecallingprogram.
IntroductiontoIterators
TheconceptofiteratorsisfundamentaltousingthecontainerclassesandtheassociatedalgorithmsprovidedbytheC++StandardLibrary.Aniteratorisapointer-likeobjectusedtocyclethroughalltheelementsstoredinacontainer.Becausedifferentalgorithmsneedtotraversecontainersinavarietyoffashions,therearedifferentformsofiterators.EachcontainerclassintheC++StandardLibrarycangenerateaniteratorwithfunctionalityappropriatetothestoragetechniqueusedinimplementingthecontainer.ItisthecategoryofiteratorsrequiredasargumentsthatchieflydistinguisheswhichalgorithmsintheC++StandardLibrarycanbeusedwithwhichcontainerclasses.
Justaspointerscanbeusedinavarietyofwaysintraditionalprogramming,iteratorscanbeusedforanumberofdifferentpurposes.Aniteratorcanbeusedtodenoteaspecificvalue,justasapointercanbeusedtoreferenceaspecificmemorylocation.Apairofiteratorscanbeusedt