ebook img

The C++ Standard Library: A Tutorial and Reference PDF

1190 Pages·2012·6.57 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The C++ Standard Library: A Tutorial and Reference

The C++ Standard Library Second Edition This page intentionally left blank The C++ Standard Library A Tutorial and Reference Second Edition Nicolai M. Josuttis UpperSaddleRiver,NJ Boston Indianapolis SanFrancisco • • • NewYork Toronto Montreal London Munich Paris Madrid • • • • • • Capetown Sydney Tokyo Singapore MexicoCity • • • • Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademarkclaim,thedesignationshavebeenprintedwithinitialcapitallettersorinallcapitals. Theauthorandpublisherhavetakencareinthepreparationofthisbook,butmakenoexpressedor impliedwarrantyof any kind and assume no responsibilityfor errorsor omissions. No liabilityis assumedforincidentalorconsequentialdamagesinconnectionwithorarisingoutoftheuseofthe informationorprogramscontainedherein. Thepublisheroffersexcellentdiscountsonthisbookwhenorderedinquantityforbulkpurchasesor specialsales,whichmayincludeelectronicversionsand/orcustomcoversandcontentparticularto yourbusiness,traininggoals,marketingfocus,andbrandinginterests.Formoreinformation,please contact: U.S.CorporateandGovernmentSales (800)382-3419 [email protected] ForsalesoutsidetheUnitedStates,pleasecontact: InternationalSales [email protected] VisitusontheWeb: informit.com/aw LibraryofCongressCataloging-in-PublicationData Josuttis,NicolaiM. TheC++standardlibrary: atutorialandreference/NicolaiM.Josuttis.—2nded. p. cm. Includesbibliographicalreferencesandindex. ISBN978-0-321-62321-8(hardcover: alk. paper) 1. C++(Computerprogramlanguage)I.Title. QA76.73.C153J692012 005.13’3-dc23 2011045071 Copyright(cid:2)c 2012PearsonEducation,Inc. ThisbookwastypesetbytheauthorusingtheLATEXdocumentprocessingsystem. Allrightsreserved. PrintedintheUnitedStatesofAmerica. Thispublicationisprotectedbycopy- right, and permission must be obtained from the publisher prior to any prohibited reproduction, storageinaretrievalsystem,ortransmissioninanyformorbyanymeans,electronic,mechanical, photocopying, recording, or likewise. To obtainpermissionto usematerialfromthiswork, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, UpperSaddleRiver,NewJersey07458,oryoumayfaxyourrequestto(201)236-3290. ISBN-13: 978-0-321-62321-8 ISBN-10: 0-321-62321-5 TextprintedintheUnitedStatesonrecycledpaperatEdwardsBrothersinAnnArbor,Michigan. Firstprinting,March2012 To those who care for people and mankind This page intentionally left blank Contents PrefacetotheSecondEdition xxiii AcknowledgmentsfortheSecondEdition xxiv PrefacetotheFirstEdition xxv AcknowledgmentsfortheFirstEdition xxvi 1 AboutThisBook 1 1.1 WhyThisBook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 BeforeReadingThisBook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 StyleandStructureoftheBook . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.4 HowtoReadThisBook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.5 StateoftheArt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.6 ExampleCodeandAdditionalInformation . . . . . . . . . . . . . . . . . . . . . 5 1.7 Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 IntroductiontoC++andtheStandardLibrary 7 2.1 HistoryoftheC++Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.1 CommonQuestionsabouttheC++11Standard . . . . . . . . . . . . . . 8 2.1.2 CompatibilitybetweenC++98andC++11 . . . . . . . . . . . . . . . . . 9 2.2 ComplexityandBig-ONotation . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3 NewLanguageFeatures 13 3.1 NewC++11LanguageFeatures . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.1.1 ImportantMinorSyntaxCleanups . . . . . . . . . . . . . . . . . . . . . 13 3.1.2 AutomaticTypeDeductionwithauto . . . . . . . . . . . . . . . . . . . 14 3.1.3 UniformInitializationandInitializerLists . . . . . . . . . . . . . . . . . 15 3.1.4 Range-BasedforLoops . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.1.5 MoveSemanticsandRvalueReferences . . . . . . . . . . . . . . . . . . 19 viii Contents 3.1.6 NewStringLiterals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.1.7 Keywordnoexcept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.1.8 Keywordconstexpr . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.1.9 NewTemplateFeatures . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.1.10 Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.1.11 Keyworddecltype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.1.12 NewFunctionDeclarationSyntax . . . . . . . . . . . . . . . . . . . . . 32 3.1.13 ScopedEnumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.1.14 NewFundamentalDataTypes . . . . . . . . . . . . . . . . . . . . . . . 33 3.2 Old“New”LanguageFeatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.2.1 ExplicitInitializationforFundamentalTypes . . . . . . . . . . . . . . . 37 3.2.2 Definitionofmain() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4 GeneralConcepts 39 4.1 Namespacestd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.2 HeaderFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.3 ErrorandExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.3.1 StandardExceptionClasses . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.3.2 MembersofExceptionClasses . . . . . . . . . . . . . . . . . . . . . . . 44 4.3.3 PassingExceptionswithClassexception_ptr . . . . . . . . . . . . . . 52 4.3.4 ThrowingStandardExceptions . . . . . . . . . . . . . . . . . . . . . . . 53 4.3.5 DerivingfromStandardExceptionClasses . . . . . . . . . . . . . . . . . 54 4.4 CallableObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.5 ConcurrencyandMultithreading. . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.6 Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5 Utilities 59 5.1 PairsandTuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 5.1.1 Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 5.1.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 5.1.3 I/OforTuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.1.4 Conversionsbetweentuplesandpairs . . . . . . . . . . . . . . . . . . 75 5.2 SmartPointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.2.1 Classshared_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.2.2 Classweak_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5.2.3 MisusingSharedPointers . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.2.4 SharedandWeakPointersinDetail. . . . . . . . . . . . . . . . . . . . . 92 5.2.5 Classunique_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Contents ix 5.2.6 Classunique_ptrinDetail . . . . . . . . . . . . . . . . . . . . . . . . 110 5.2.7 Classauto_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 5.2.8 FinalWordsonSmartPointers . . . . . . . . . . . . . . . . . . . . . . . 114 5.3 NumericLimits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5.4 TypeTraitsandTypeUtilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5.4.1 PurposeofTypeTraits . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5.4.2 TypeTraitsinDetail. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 5.4.3 ReferenceWrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 5.4.4 FunctionTypeWrappers . . . . . . . . . . . . . . . . . . . . . . . . . . 133 5.5 AuxiliaryFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 5.5.1 ProcessingtheMinimumandMaximum . . . . . . . . . . . . . . . . . . 134 5.5.2 SwappingTwoValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 5.5.3 SupplementaryComparisonOperators . . . . . . . . . . . . . . . . . . . 138 5.6 Compile-TimeFractionalArithmeticwithClassratio<> . . . . . . . . . . . . . 140 5.7 ClocksandTimers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 5.7.1 OverviewoftheChronoLibrary . . . . . . . . . . . . . . . . . . . . . . 143 5.7.2 Durations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 5.7.3 ClocksandTimepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 5.7.4 DateandTimeFunctionsbyCandPOSIX . . . . . . . . . . . . . . . . . 157 5.7.5 BlockingwithTimers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 5.8 HeaderFiles<cstddef>,<cstdlib>,and<cstring> . . . . . . . . . . . . . . 161 5.8.1 Definitionsin<cstddef> . . . . . . . . . . . . . . . . . . . . . . . . . . 161 5.8.2 Definitionsin<cstdlib> . . . . . . . . . . . . . . . . . . . . . . . . . . 162 5.8.3 Definitionsin<cstring> . . . . . . . . . . . . . . . . . . . . . . . . . . 163 6 TheStandardTemplateLibrary 165 6.1 STLComponents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 6.2 Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 6.2.1 SequenceContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 6.2.2 AssociativeContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 6.2.3 UnorderedContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 6.2.4 AssociativeArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 6.2.5 OtherContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 6.2.6 ContainerAdapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 6.3 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 6.3.1 FurtherExamplesofUsingAssociativeandUnorderedContainers . . . . 193 6.3.2 IteratorCategories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.