编辑推荐
新标准C++11发布,距上一版本已10年;《C++Primer英文版(第5版)》是持续更新的全球C++读本。
经过前四个版本积累,第5版的体例堪称完美。
这一版本作者历时3年完成,极力避免在原版上升级,而是将C++11的新特性真正融入各章节;更将所有代码示例用C++11的简化写法完成,而不是仅单独增加内容。
内容简介
《C++Primer英文版(第5版)》久负盛名的C++经典教程,时隔八年之久,终迎来的重大升级。除令全球无数程序员从中受益,甚至为之迷醉的--C++大师StanleyB.Lippman的丰富实践经验,C++标准委员会原负责人JoséeLajoie对C++标准的深入理解,以及C++先驱BarbaraE.Moo在C++教学方面的真知灼见外,更是基于全新的C++11标准进行了全面而彻底的内容更新。非常难能可贵的是,书中所有示例均全部采用C++11标准改写,这在经典升级版中极其罕见--充分体现了C++语言的重大进展极其全面实践。书中丰富的教学辅助内容、醒目的知识点提示,以及精心组织的编程示范,让《C++Primer英文版(第5版)》这《C++ Primer英文版(第5版)》在C++领域的专业地位更加不可动摇。无论是初学者入门,或是中、高级程序员提升,《C++Primer英文版(第5版)》均为不容置疑的优选。
作者简介
StanleyB.Lippman,目前是微软公司VisualC++团队的架构师。他从1984年开始在贝尔实验室与C++的设计者BjarneStroustrup一起从事C++的设计与开发。他在迪士尼和梦工厂从事动画制作,还担任过JPL的高级顾问。
JoséeLajoie,曾经是IBM加拿大研究中心C/C++编译器开发团队的成员,在ISOC++标准委员会工作了7年,担任过ISO核心语言工作组的主席和C++Report杂志的专栏作家。
BarbaraE.Moo,拥有25年软件经验的独立咨询顾问。在AT&T,她与Stroustrup、Lippman一起管理过复杂的C++开发项目。
目录
Prefacexxiii
Chapter1GettingStarted
1.1WritingaSimpleC++Program
1.1.1CompilingandExecutingOurProgram
1.2AFirstLookatInput/Output
1.3AWordaboutComments
1.4FlowofControl
1.4.1ThewhileStatement
1.4.2TheforStatement
1.4.3ReadinganUnknownNumberofInputs
1.4.4TheifStatement
1.5IntroducingClasses
1.5.1TheSales_itemClass
1.5.2AFirstLookatMemberFunctions
1.6TheBookstoreProgram
ChapterSummary
DefinedTerms
PartITheBasics
Chapter2VariablesandBasicTypes
2.1PrimitiveBuilt-inTypes
2.1.1ArithmeticTypes
2.1.2TypeConversions
2.1.3Literals
2.2Variables
2.2.1VariableDefinitions
2.2.2VariableDeclarationsandDefinitions
2.2.3Identifiers
2.2.4ScopeofaName
2.3CompoundTypes
2.3.1References
2.3.2Pointers
vii
viiiContents
2.3.3UnderstandingCompoundTypeDeclarations
2.4constQualifier
2.4.1Referencestoconst
2.4.2Pointersandconst
2.4.3Top-Levelconst
2.4.4constexprandConstantExpressions
2.5DealingwithTypes
2.5.1TypeAliases
2.5.2TheautoTypeSpecifier
2.5.3ThedecltypeTypeSpecifier
2.6DefiningOurOwnDataStructures
2.6.1DefiningtheSales_dataType
2.6.2UsingtheSales_dataClass
2.6.3WritingOurOwnHeaderFiles
ChapterSummary
DefinedTerms
Chapter3Strings,Vectors,andArrays
3.1NamespaceusingDeclarations
3.2LibrarystringType
3.2.1DefiningandInitializingstrings
3.2.2Operationsonstrings
3.2.3DealingwiththeCharactersinastring
3.3LibraryvectorType
3.3.1DefiningandInitializingvectors
3.3.2AddingElementstoavector
3.3.3OthervectorOperations
3.4IntroducingIterators
3.4.1UsingIterators
3.4.2IteratorArithmetic
3.5Arrays
3.5.1DefiningandInitializingBuilt-inArrays
3.5.2AccessingtheElementsofanArray
3.5.3PointersandArrays
3.5.4C-StyleCharacterStrings
3.5.5InterfacingtoOlderCode
3.6MultidimensionalArrays
ChapterSummary
DefinedTerms
Chapter4Expressions
4.1Fundamentals
4.1.1BasicConcepts
4.1.2PrecedenceandAssociativity
4.1.3OrderofEvaluation
4.2ArithmeticOperators
4.3LogicalandRelationalOperators
Contentsix
4.4AssignmentOperators
4.5IncrementandDecrementOperators
4.6TheMemberAccessOperators
4.7TheConditionalOperator
4.8TheBitwiseOperators
4.9ThesizeofOperator
4.10CommaOperator
4.11TypeConversions
4.11.1TheArithmeticConversions
4.11.2OtherImplicitConversions
4.11.3ExplicitConversions
4.12OperatorPrecedenceTable
ChapterSummary
DefinedTerms
Chapter5Statements
5.1SimpleStatements
5.2StatementScope
5.3ConditionalStatements
5.3.1TheifStatement
5.3.2TheswitchStatement
5.4IterativeStatements
5.4.1ThewhileStatement
5.4.2TraditionalforStatement
5.4.3RangeforStatement
5.4.4ThedowhileStatement
5.5JumpStatements
5.5.1ThebreakStatement
5.5.2ThecontinueStatement
5.5.3ThegotoStatement
5.6tryBlocksandExceptionHandling
5.6.1AthrowExpression
5.6.2ThetryBlock
5.6.3StandardExceptions
ChapterSummary
DefinedTerms
Chapter6Functions
6.1FunctionBasics
6.1.1LocalObjects
6.1.2FunctionDeclarations
6.1.3SeparateCompilation
6.2ArgumentPassing
6.2.1PassingArgumentsbyValue
6.2.2PassingArgumentsbyReference
6.2.3constParametersandArguments
6.2.4ArrayParameters
xContents
6.2.5main:HandlingCommand-LineOptions
6.2.6FunctionswithVaryingParameters
6.3ReturnTypesandthereturnStatement
6.3.1FunctionswithNoReturnValue
6.3.2FunctionsThatReturnaValue
6.3.3ReturningaPointertoanArray
6.4OverloadedFunctions
6.4.1OverloadingandScope
6.5FeaturesforSpecializedUses
6.5.1DefaultArguments
6.5.2InlineandconstexprFunctions
6.5.3AidsforDebugging
6.6FunctionMatching
6.6.1ArgumentTypeConversions
6.7PointerstoFunctions
ChapterSummary
DefinedTerms
Chapter7Classes
7.1DefiningAbstractDataTypes
7.1.1DesigningtheSales_dataClass
7.1.2DefiningtheRevisedSales_dataClass
7.1.3DefiningNonmemberClass-RelatedFunctions
7.1.4Constructors
7.1.5Copy,Assignment,andDestruction
7.2AccessControlandEncapsulation
7.2.1Friends
7.3AdditionalClassFeatures
7.3.1ClassMembersRevisited
7.3.2FunctionsThatReturn*this
7.3.3ClassTypes
7.3.4FriendshipRevisited
7.4ClassScope
7.4.1NameLookupandClassScope
7.5ConstructorsRevisited
7.5.1ConstructorInitializerList
7.5.2DelegatingConstructors
7.5.3TheRoleoftheDefaultConstructor
7.5.4ImplicitClass-TypeConversions
7.5.5AggregateClasses
7.5.6LiteralClasses
7.6staticClassMembers
ChapterSummary
DefinedTerms
Contentsxi
PartIITheC++Library
Chapter8TheIOLibrary
8.1TheIOClasses
8.1.1NoCopyorAssignforIOObjects
8.1.2ConditionStates
8.1.3ManagingtheOutputBuffer
8.2FileInputandOutput
8.2.1UsingFileStreamObjects
8.2.2FileModes
8.3stringStreams
8.3.1Usinganistringstream
8.3.2Usingostringstreams
ChapterSummary
DefinedTerms
Chapter9SequentialContainers
9.1OverviewoftheSequentialContainers
9.2ContainerLibraryOverview
9.2.1Iterators
9.2.2ContainerTypeMembers
9.2.3beginandendMembers
9.2.4DefiningandInitializingaContainer
9.2.5Assignmentandswap
9.2.6ContainerSizeOperations
9.2.7RelationalOperators
9.3SequentialContainerOperations
9.3.1AddingElementstoaSequentialContainer
9.3.2AccessingElements
9.3.3ErasingElements
9.3.4Specializedforward_listOperations
9.3.5ResizingaContainer
9.3.6ContainerOperationsMayInvalidateIterators
9.4HowavectorGrows
9.5AdditionalstringOperations
9.5.1OtherWaystoConstructstrings
9.5.2OtherWaystoChangeastring
9.5.3stringSearchOperations
9.5.4ThecompareFunctions
9.5.5NumericConversions
9.6ContainerAdaptors
ChapterSummary
DefinedTerms
xiiContents
Chapter10GenericAlgorithms
10.1Overview
10.2AFirstLookattheAlgorithms
10.2.1Read-OnlyAlgorithms
10.2.2AlgorithmsThatWriteContainerElements
10.2.3AlgorithmsThatReorderContainerElements
10.3CustomizingOperations
10.3.1PassingaFunctiontoanAlgorithm
10.3.2LambdaExpressions
10.3.3LambdaCapturesandReturns
10.3.4BindingArguments
10.4RevisitingIterators
10.4.1InsertIterators
10.4.2iostreamIterators
10.4.3ReverseIterators
10.5StructureofGenericAlgorithms
10.5.1TheFiveIteratorCategories
10.5.2AlgorithmParameterPatterns
10.5.3AlgorithmNamingConventions
10.6Container-SpecificAlgorithms
ChapterSummary
DefinedTerms
Chapter11AssociativeContainers
11.1UsinganAssociativeContainer
11.2OverviewoftheAssociativeContainers
11.2.1DefininganAssociativeContainer
11.2.2RequirementsonKeyType
11.2.3ThepairType
11.3OperationsonAssociativeContainers
11.3.1AssociativeContainerIterators
11.3.2AddingElements
11.3.3ErasingElements
11.3.4Subscriptingamap
11.3.5AccessingElements
11.3.6AWordTransformationMap
11.4TheUnorderedContainers
ChapterSummary
DefinedTerms
Chapter12DynamicMemory
12.1DynamicMemoryandSmartPointers
12.1.1Theshared_ptrClass
12.1.2ManagingMemoryDirectly
12.1.3Usingshared_ptrswithnew
12.1.4SmartPointersandExceptions
12.1.5unique_ptr
Contentsxiii
12.1.6weak_ptr
12.2DynamicArrays
12.2.1newandArrays
12.2.2TheallocatorClass
12.3UsingtheLibrary:AText-QueryProgram
12.3.1DesignoftheQueryProgram
12.3.2DefiningtheQueryProgramClasses
ChapterSummary
DefinedTerms
PartIIIToolsforClassAuthors
Chapter13CopyControl
13.1Copy,Assign,andDestroy
13.1.1TheCopyConstructor
13.1.2TheCopy-AssignmentOperator
13.1.3TheDestructor
13.1.4TheRuleofThree/Five
13.1.5Using=default
13.1.6PreventingCopies
13.2CopyControlandResourceManagement
13.2.1ClassesThatActLikeValues
13.2.2DefiningClassesThatActLikePointers
13.3Swap
13.4ACopy-ControlExample
13.5ClassesThatManageDynamicMemory
13.6MovingObjects
13.6.1RvalueReferences
13.6.2MoveConstructorandMoveAssignment
13.6.3RvalueReferencesandMemberFunctions
ChapterSummary
DefinedTerms
Chapter14OverloadedOperationsandConversions
14.1BasicConcepts
14.2InputandOutputOperators
14.2.1OverloadingtheOutputOperator>
14.3ArithmeticandRelationalOperators
14.3.1EqualityOperators
14.3.2RelationalOperators
14.4AssignmentOperators
14.5SubscriptOperator
14.6IncrementandDecrementOperators
14.7MemberAccessOperators
14.8Function-CallOperator
xivContents
14.8.1LambdasAreFunctionObjects
14.8.2Library-DefinedFunctionObjects
14.8.3CallableObjectsandfunction
14.9Overloading,Conversions,andOperators
14.9.1ConversionOperators
14.9.2AvoidingAmbiguousConversions
14.9.3FunctionMatchingandOverloadedOperators
ChapterSummary
DefinedTerms
Chapter15Object-OrientedProgramming
15.1OOP:AnOverview
15.2DefiningBaseandDerivedClasses
15.2.1DefiningaBaseClass
15.2.2DefiningaDerivedClass
15.2.3ConversionsandInheritance
15.3VirtualFunctions
15.4AbstractBaseClasses
15.5AccessControlandInheritance
15.6ClassScopeunderInheritance
15.7ConstructorsandCopyControl
15.7.1VirtualDestructors
15.7.2SynthesizedCopyControlandInheritance
15.7.3Derived-ClassCopy-ControlMembers
15.7.4InheritedConstructors
15.8ContainersandInheritance
15.8.1WritingaBasketClass
15.9TextQueriesRevisited
15.9.1AnObject-OrientedSolution
15.9.2TheQuery_baseandQueryClasses
15.9.3TheDerivedClasses
15.9.4TheevalFunctions
ChapterSummary
DefinedTerms
Chapter16TemplatesandGenericProgramming
16.1DefiningaTemplate
16.1.1FunctionTemplates
16.1.2ClassTemplates
16.1.3TemplateParameters
16.1.4MemberTemplates
16.1.5ControllingInstantiations
16.1.6EfficiencyandFlexibility
16.2TemplateArgumentDeduction
16.2.1ConversionsandTemplateTypeParameters
16.2.2Function-TemplateExplicitArguments
16.2.3TrailingReturnTypesandTypeTransformation
Contentsxv
16.2.4FunctionPointersandArgumentDeduction
16.2.5TemplateArgumentDeductionandReferences
16.2.6Understandingstd::move
16.2.7Forwarding
16.3OverloadingandTemplates
16.4VariadicTemplates
16.4.1WritingaVariadicFunctionTemplate
16.4.2PackExpansion
16.4.3ForwardingParameterPacks
16.5TemplateSpecializations
ChapterSummary
DefinedTerms
PartIVAdvancedTopics
Chapter17SpecializedLibraryFacilities
17.1ThetupleType
17.1.1DefiningandInitializingtuples
17.1.2UsingatupletoReturnMultipleValues
17.2ThebitsetType
17.2.1DefiningandInitializingbitsets
17.2.2Operationsonbitsets
17.3RegularExpressions
17.3.1UsingtheRegularExpressionLibrary
17.3.2TheMatchandRegexIteratorTypes
17.3.3UsingSubexpressions
17.3.4Usingregex_replace
17.4RandomNumbers
17.4.1Random-NumberEnginesandDistribution
17.4.2OtherKindsofDistributions
17.5TheIOLibraryRevisited
17.5.1FormattedInputandOutput
17.5.2UnformattedInput/OutputOperations
17.5.3RandomAccesstoaStream
ChapterSummary
DefinedTerms
Chapter18ToolsforLargePrograms
18.1ExceptionHandling
18.1.1ThrowinganException
18.1.2CatchinganException
18.1.3FunctiontryBlocksandConstructors
18.1.4ThenoexceptExceptionSpecification
18.1.5ExceptionClassHierarchies
18.2Namespaces
18.2.1NamespaceDefinitions
xviContents
18.2.2UsingNamespaceMembers
18.2.3Classes,Namespaces,andScope
18.2.4OverloadingandNamespaces
18.3MultipleandVirtualInheritance
18.3.1MultipleInheritance
18.3.2ConversionsandMultipleBaseClasses
18.3.3ClassScopeunderMultipleInheritance
18.3.4VirtualInheritance
18.3.5ConstructorsandVirtualInheritance
ChapterSummary
DefinedTerms
Chapter19SpecializedToolsandTechniques
19.1ControllingMemoryAllocation
19.1.1Overloadingnewanddelete
19.1.2PlacementnewExpressions
19.2Run-TimeTypeIdentification
19.2.1Thedynamic_castOperator
19.2.2ThetypeidOperator
19.2.3UsingRTTI
19.2.4Thetype_infoClass
19.3Enumerations
19.4PointertoClassMember
19.4.1PointerstoDataMembers
19.4.2PointerstoMemberFunctions
19.4.3UsingMemberFunctionsasCallableObjects
19.5NestedClasses
19.6union:ASpace-SavingClass
19.7LocalClasses
19.8InherentlyNonportableFeatures
19.8.1Bit-fields
19.8.2volatileQualifier
19.8.3LinkageDirectives:extern"C"
ChapterSummary
DefinedTerms
AppendixATheLibrary
A.1LibraryNamesandHeaders
A.2ABriefTouroftheAlgorithms
A.2.1AlgorithmstoFindanObject
A.2.2OtherRead-OnlyAlgorithms
A.2.3BinarySearchAlgorithms
A.2.4AlgorithmsThatWriteContainerElements
A.2.5PartitioningandSortingAlgorithms
A.2.6GeneralReorderingOperations
A.2.7PermutationAlgorithms
A.2.8SetAlgorithmsforSortedSequences
Contentsxvii
A.2.9MinimumandMaximumValues
A.2.10NumericAlgorithms
A.3RandomNumbers
A.3.1RandomNumberDistributions
A.3.2RandomNumberEngines
Index
NewFeaturesinC++
2.1.1longlongType
2.2.1ListInitialization
2.3.2nullptrLiteral
2.4.4constexprVariables
2.5.1TypeAliasDeclarations
2.5.2TheautoTypeSpecifier
2.5.3ThedecltypeTypeSpecifier
2.6.1In-ClassInitializers
3.2.2UsingautoordecltypeforTypeAbbreviation
3.2.3RangeforStatement
3.3Definingavectorofvectors
3.3.1ListInitializationforvectors
3.4.1ContainercbeginandcendFunctions
3.5.3LibrarybeginandendFunctions
3.6UsingautoordecltypetoSimplifyDeclarations
4.2RoundingRulesforDivision
4.4AssignmentfromaBracedListofValues
4.9sizeofAppliedtoaClassMember
5.4.3RangeforStatement
6.2.6Libraryinitializer_listClass
6.3.2ListInitializingaReturnValue
6.3.3DeclaringaTrailingReturnType
6.3.3UsingdecltypetoSimplifyReturnTypeDeclarations
6.5.2constexprFunctions
7.1.4Using=defaulttoGenerateaDefaultConstructor
7.3.1In-classInitializersforMembersofClassType
7.5.2DelegatingConstructors
7.5.6constexprConstructors
8.2.1UsingstringsforFileNames
9.1Thearrayandforward_listContainers
9.2.3ContainercbeginandcendFunctions
9.2.4ListInitializationforContainers
9.2.5ContainerNonmemberswapFunctions
9.3.1ReturnTypeforContainerinsertMembers
9.3.1ContaineremplaceMembers
xix
xxNewFeaturesinC++
9.4shrink_to_fit
9.5.5NumericConversionFunctionsforstrings
10.3.2LambdaExpressions
10.3.3TrailingReturnTypeinLambdaExpressions
10.3.4TheLibrarybindFunction
11.2.1ListInitializationofanAssociativeContainer
11.2.3ListInitializingpairReturnType
11.3.2ListInitializationofapair
11.4TheUnorderedContainers
12.1SmartPointers
12.1.1Theshared_ptrClass
12.1.2ListInitializationofDynamicallyAllocatedObjects
12.1.2autoandDynamicAllocation
12.1.5Theunique_ptrClass
12.1.6Theweak_ptrClass
12.2.1RangeforDoesn'tApplytoDynamicallyAllocatedArrays
12.2.1ListInitializationofDynamicallyAllocatedArrays
12.2.1autoCan'tBeUsedtoAllocateanArray
12.2.2allocator::constructCanUseanyConstructor
13.1.5Using=defaultforCopy-ControlMembers
13.1.6Using=deletetoPreventCopyingClassObjects
13.5MovingInsteadofCopyingClassObjects
13.6.1RvalueReferences
13.6.1TheLibrarymoveFunction
13.6.2MoveConstructorandMoveAssignment
13.6.2MoveConstructorsUsuallyShouldBenoexcept
13.6.2MoveIterators
13.6.3ReferenceQualifiedMemberFunctions
14.8.3ThefunctionClassTemplate
14.9.1explicitConversionOperators
15.2.2overrideSpecifierforVirtualFunctions
15.2.2PreventingInheritancebyDefiningaClassasfinal
15.3overrideandfinalSpecifiersforVirtualFunctions
15.7.2DeletedCopyControlandInheritance
15.7.4InheritedConstructors
16.1.2DeclaringaTemplateTypeParameterasaFriend
16.1.2TemplateTypeAliases
16.1.3DefaultTemplateArgumentsforTemplateFunctions
16.1.5ExplicitControlofInstantiation
16.2.3TemplateFunctionsandTrailingReturnTypes
16.2.5ReferenceCollapsingRules
16.2.6static_castfromanLvaluetoanRvalue
16.2.7TheLibraryforwardFunction
16.4VariadicTemplates
16.4Thesizeof...Operator
16.4.3VariadicTemplatesandForwarding
NewFeaturesinC++11xxi
17.1TheLibraryTupleClassTemplate
17.2.2NewbitsetOperations
17.3TheRegularExpressionLibrary
17.4TheRandomNumberLibrary
17.5.1Floating-PointFormatControl
18.1.4ThenoexceptExceptionSpecifier
18.1.4ThenoexceptOperator
18.2.1InlineNamespaces
18.3.1InheritedConstructorsandMultipleInheritance
19.3Scopedenums
19.3SpecifyingtheTypeUsedtoHoldanenum
19.3ForwardDeclarationsforenums
19.4.3TheLibrarymem_fnClassTemplate
19.6UnionMembersofClassTypes
精彩书摘
" Westartbyinitializingittodenotethefirstelementintext.Theloopcontinuesuntileitherweprocesseveryelementintextorwefindanelementthatisempty.Solongasthereareelementsandwehaven'tseenanemptyelement,weprintthecurrentelement.Itisworthnotingthatbeeausetheloopreadsbutdoesnotwritetotheelementsintext,weusecbeginandcendtocontroltheiteration.SomevectorOperationsInvalidateIterators
In3.3.2(p.101)wenotedthatthereareimplicationsofthefactthatvectorscangrowdynamically.Wealsonotedthatonesuchimplicationisthatwecannotaddelementstoavectorinsidearangeforloop.Anotherimplicationisthatanyoperation,suchaspush_back,thatchangesthesizeofavectorpotentiallyinvalidatesalliteratorsintothatvector.We'llexplorehowiteratorsbecomeinvalidinmoredetailin9.3.6(p.353).
WARNING
Fornow,itisimportanttorealizethatloopsthatuseiteratorsshouldnotaddelementstothecontainertowhichtheiteratorsrefer.
EXERCISESSECTION3.4.1
Exercise3.21:Redothefirstexercisefrom3.3.3(p.105)usingiterators.
Exercise3.22:Revisetheloopthatprintedthefirstparagraphintexttoinsteadchangetheelementsintextthatcorrespondtothefirstparagraphtoalluppercase.Afteryou'veupdatedtext,printitscontents.
Exercise3.23:Writeaprogramtocreateavectorwithtenintelements.Usinganiterator,assigneachelementavaluethatistwiceitscurrentvalue.Testyourprogrambyprintingthevector."
……
前言/序言
难以计数的程序员已经通过旧版的C++Primer学会了C++语言。而在这段时间中,C++本身又已成熟了许多:语言本身的关注点和程序设计社区的关注点都已大大开阔,已经从主要关注机器效率转变为更多地关注编程效率。
2011年,C++标准委员会发布了ISOC++标准的一个重要修订版。此修订版是C++进化过程中的最新一步,延续了前几个版本对编程效率的强调。新标准的主要目标是:
使语言更为统一,更易于教学
使标准库更简单、安全、使用更高效
使编写高效率的抽象和库变得更简单
因此,在这个版本的C++Primer中,我们进行了彻底的修改,使用了最新的C++标准。为了了解新标准是如何全面影响C++语言的,你可以看一下xxiii页至xxv页的新特性列表,其中列出了哪些章节涉及了C++的新特性。
新标准增加的一些特性是具有普适性的,例如用于类型推断的auto。这些新特性使《C++ Primer英文版(第5版)》中的代码更易于阅读和理解。程序(以及程序员!)可以忽略类型的细节,从而更容易集中精力于程序逻辑上来。其他一些新特性,例如智能指针和允许移动的容器,允许我们编写更为复杂的类,而又不必与错综复杂的资源管理做斗争。因此,在《C++ Primer英文版(第5版)》中开始讲授如何编写自己的类,会比第4版简单得多。旧标准中阻挡在我们前进路上的很多细节,你我都不必再担心了。
对于《C++ Primer英文版(第5版)》中涉及新标准定义的新特性的那些部分,我们都已用一个特殊的图标标记出来了。我们希望这些提示标记对那些已经熟悉C++语言核心内容的读者是有帮助的,可以帮助他们决定将注意力投向哪里。对于那些可能尚不支持所有新特性的编译器,我们还希望这些图标能有助于解释这类编译器所给出的编译错误信息。这是因为虽然《C++ Primer英文版(第5版)》中几乎所有例子都已经用最新版本的GNU编译器编译通过,但我们知道一些读者可能尚未将编译器更新到最新版本。虽然新标准增加了大量新功能,但核心C++语言并未变化,这构成了《C++ Primer英文版(第5版)》的大部分内容。读者可以借助这些图标来判断哪些功能可能还没有被自己的编译器所支持。
为什么选择这《C++ Primer英文版(第5版)》?
现代C++语言可以看作是三部分组成的:
低级语言,大部分继承自C语言。
现代高级语言特性,允许我们定义自己的类型以及组织大规模程序和系统。
标准库,它利用高级特性来提供有用的数据结构和算法。
大多数C++教材按照语言进化的顺序来组织其内容。首先讲授C++的C子集,然后将C++的更为抽象的一些特性作为高级话题在书的最后进行介绍。这种方式存在两个问题:读者会陷入那些继承自低级程序设计的细节,从而由于挫折感而放弃;读者被强加学习一些坏习惯,随后又需要忘记这些内容。
我们采用一种相反的方法:从一开始就介绍一些语言特性,能让程序员忽略那些继承自低级程序设计的细节。例如,在介绍和使用内置的算术和数组类型时,我们还连同介绍和使用标准库中的类型string和vector。使用这些类型的程序更易写、易理解且更少出错。
太多时候,标准库被当作一种“高级”话题来讲授。很多教材不使用标准库,而是使用基于字符数组指针和动态内存管理的低级程序设计技术。让使用这种低级技术的程序正确运行,要比编写相应的使用标准库的C++代码困难得多。
贯穿C++Primer《C++ Primer英文版(第5版)》,我们都在强调好的风格:我们想帮助读者直接养成好的习惯,而不是在获得很多很复杂的知识后再去忘掉那些坏习惯。我们特别强调那些棘手的问题,并对常见的错误想法和陷阱提出警告。
我们还注意解释规则背后的基本原理--使读者不仅知其然,还能知其所以然。我们相信,通过体会程序的工作原理,读者会更快地巩固对语言的理解。
虽然你不必为了学习《C++ Primer英文版(第5版)》而掌握C语言,但我们还是假定你了解足够多的程序设计知识,了解至少一门现代分程序结构语言,知道如何用这门语言编写、编译以及运行程序。特别是,我们假定你已经使用过变量,编写、调用过函数,也使用过编译器。
第5版变化的内容
这一版C++Primer的新特点是用边栏图标来帮助引导读者。C++是一种庞大的编程语言,它提供了一些为特定程序设计问题定制的功能。其中一些功能对大型项目团队有很重要的意义,但对于小型项目开发可能并无必要。因此,并非每个程序员都需要了解每个语言特性的所有细节。我们加入这些边栏图标来帮助读者弄清哪些内容可以随后再学习,而哪些主题是更为重要的。
对于包含C++语言基础内容的章节,我们用一个小人正在读书的图标加以标记。用这个图标标记的那些章节,涵盖了构成语言核心部分的主题。每个人都应该阅读并理解这些章节的内容。
对于那些涉及高级主题或特殊目的主题的章节,我们也进行了标记。在首次阅读时,这些章节可以跳过或快速浏览。我们用一叠书的图标标记这些章节,指出在这些地方,你可以放心地放下书本。快速浏览这些章节可能是一个好主意,这样你就可以知道有这些特性存在。但在真正需要在自己的程序中使用这些特性之前,没有必要花费时间仔细学习这些主题。
为了进一步引导读者的注意力,我们还用放大镜图标标记了特别复杂的概念。我们希望读者对有这种标记的章节能多花费一些时间彻底理解其中的内容。在这些章节中,至少有一些,其主题的重要性可能不是那么明显;但我们认为,你会发现这些章节涉及的主题对理解C++语言原来至关重要。
交叉引用的广泛使用,是《C++ Primer英文版(第5版)》采用的另外一种阅读帮助。我们希望这些引用能帮助读者容易地翻阅书中的内容,同时还能在后面的例子涉及到前面的内容时容易地跳回到前面。
没有改变的是,C++Primer仍是一本清晰、正确、全面的C++入门教材。我们通过给出一系列复杂度逐步增加的例子来讲授这门语言,这些例子说明了语言特性,展示了如何充分用好C++语言。
《C++ Primer英文版(第5版)》的结构
我们首先在第I部分和第II部分中介绍了C++语言和标准库的基础内容。这两部分包含的内容足够你编写出有意义的程序,而不是只能写一些玩具程序。大部分程序员基本上都需要掌握《C++ Primer英文版(第5版)》这两部分所包含的所有内容。
除了讲授C++的基础内容,第I部分和第II部分还有另外一个重要目的:通过使用标准库中定义的抽象设施,使你更加适应高级程序设计技术。标准库设施本身是一组抽象数据类型,通常用C++编写。用来设计标准库的,就是任何C++程序员都可以使用的用来构造类的那些语言特性。我们讲授C++语言的一个经验是,在先学习了使用设计良好的抽象类型后,读者会发现理解如何构造自己的类型更容易了。
只有在经过全面的标准库使用训练,并编写了各种标准库所支持的抽象程序后,我们才真正进入到那些允许你编写自己的抽象类型的C++特性中去。《C++ Primer英文版(第5版)》的第III部分和第IV部分介绍了如何编写类的形式的抽象类型。第III部分包含基础内容,第IV部分介绍更专门的语言特性。
在第III部分中,我们将介绍拷贝控制问题,以及其他一些使类能像内置类型一样容易使用的技术。类是面向对象编程和泛型编程的基础,第III部分也会介绍这些内容。第IV部分是C++Primer的结束部分,它介绍了一些在组织大型复杂系统时非常有用的语言特性。此外,我们将在附录A中总结标准库算法。
读者帮助
《C++ Primer英文版(第5版)》的每一章均以一个总结和一个术语表结束,两者一起扼要回顾了这一章的大部分学习重点。读者应该将这些部分作为个人备忘录:如果你不理解某个术语,可以重新学习这一章的相应部分。
在《C++ Primer英文版(第5版)》中我们还使用了其他一些学习辅助:
重要的术语用加粗字体显示;我们假定读者已经熟悉的重要术语用粗斜体显示。每个术语都会列在章末尾的术语表中。
贯穿《C++ Primer英文版(第5版)》,我们用高亮显示来提醒读者注意语言的重要部分,对常见的陷阱提出警告,建议好的程序设计习惯,以及提供一般性的使用提示。
为了更好地理解语言特性间和概念间的联系,我们提供大量向前的和向后的交叉引用。
对重要的概念和C++新程序员常常觉得最困难的主题,我们提供边栏讨论。
学习任何程序设计语言都需要编写程序。为此,贯穿《C++ Primer英文版(第5版)》我们提供大量程序示例。扩展示例的源码可从下面的网址获得http://www.informit.com/title/0321714113
关于编译器的注意事项
在撰写《C++ Primer英文版(第5版)》时(2012年7月),编译器提供商正在努力工作,升级编译器以匹配最新的ISO标准。我们使用最多的编译器是GNU编译器4.7.0。《C++ Primer英文版(第5版)》中只有一小部分特性在此编译器中尚未实现:继承构造函数、成员函数的引用限定符以及正则表达式库。
致谢
我们要特别感谢标准委员会几位现任和前任委员:DaveAbrahams、AndyKoenig、StephanT.Lavavej、JasonMerrill、JohnSpicer和HerbSutter在准备《C++ Primer英文版(第5版)》的过程中提供的帮助。在理解新标准的一些更微妙之处,他们为我们提供了宝贵的帮助。我们还要感谢很多致力于升级GNU编译器以实现新标准的人们。
与旧版C++Primer中一样,我们要感谢BjarneStroustrup不知疲倦地为C++工作以及他和作者长时间的友谊。我们还要感谢AlexStepanov的非凡洞察力,催生了标准库核心的容器和算法。最后,我们要感谢C++标准委员会的所有委员,感谢他们这么多年来在净化、精炼和改进C++语言方面的辛苦工作。
我们衷心感谢审稿人:MarshallClow、JonKalb、NevinLiber、Dr.C.L.Tondo、DaveedVandevoorde和SteveVinoski,他们建设性的意见帮助我们对《C++ Primer英文版(第5版)》做出了大大小小的改进。
《C++ Primer英文版(第5版)》是用Latex及其发行版本中的很多包来进行排版的,我们应该感谢Latex社区成员创造出如此强大的排版工具。
最后,我们要感谢Addison-Wesley公司的优秀员工,他们指导了《C++ Primer英文版(第5版)》的整个出版过程:PeterGordon,我们的编辑,他给了我们动力再次修改C++Primer;KimBoedigheimer,保证了一切按计划进行;BarbaraWood,她在编辑过程中找到了大量编辑错误;还有ElizabethRyan,很高兴再次和她共同工作,她指导我们完成了整个设计和生产流程。