Beginning C++17, 5th edition

788 1.1K 0
Beginning C++17, 5th edition

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Đây là cuốn sách lập trình C++ từ cơ bản đến nâng cao.Giúp bạn trở thành một Dev c++ chuyên nghiệp.Nôi dung ngắn gọn đầy đủ.Đáo ứng các nhu cầu của nhà tuyển dụng.Thông qua cuốn sách này bạn sẽ lập trình c++ hết sức chuyên nghiệp.Sách này còn bổ sung các kiến thức thực tế bạn cần trang bị để học lập trình.

Beginning C++17 From Novice to Professional — Fifth Edition — Ivor Horton Peter Van Weert www.allitebooks.com Beginning C++17 From Novice to Professional Fifth Edition Ivor Horton Peter Van Weert www.allitebooks.com Beginning C++17: From Novice to Professional Ivor Horton Peter Van Weert Stratford-upon-Avon, Warwickshire, United Kingdom Kessel-Lo, Belgium ISBN-13 (pbk): 978-1-4842-3365-8 https://doi.org/10.1007/978-1-4842-3366-5 ISBN-13 (electronic): 978-1-4842-3366-5 Library of Congress Control Number: 2018936369 Copyright © 2018 by Ivor Horton and Peter Van Weert This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Trademarked names, logos, and images may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc) SSBM Finance Inc is a Delaware corporation For information on translations, please e-mail editorial@apress.com; for reprint, paperback, or audio rights, please email bookpermissions@springernature.com Apress titles may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484233658 For more detailed information, please visit www.apress.com/source-code Printed on acid-free paper www.allitebooks.com This is for Alexander and Henry who are both going to learn programming soon If their amazing expertise with Minecraft is anything to go by, they will be brilliant at it —Ivor Horton For my wonderful family For all your love and support For putting up with me never having the time to help out around the house or to play with the train set I got you for Christmas —Peter Van Weert www.allitebooks.com Contents About the Authors�������������������������������������������������������������������������������������������������xxiii About the Technical Reviewer�������������������������������������������������������������������������������xxv Introduction���������������������������������������������������������������������������������������������������������xxvii ■Chapter ■ 1: Basic Ideas������������������������������������������������������������������������������������������� Modern C++��������������������������������������������������������������������������������������������������������������������� Standard Libraries������������������������������������������������������������������������������������������������������������ C++ Program Concepts���������������������������������������������������������������������������������������������������� Source Files and Header Files���������������������������������������������������������������������������������������������������������������� Comments and Whitespace�������������������������������������������������������������������������������������������������������������������� Preprocessing Directives and Standard Library Headers����������������������������������������������������������������������� Functions������������������������������������������������������������������������������������������������������������������������������������������������ Statements��������������������������������������������������������������������������������������������������������������������������������������������� Data Input and Output���������������������������������������������������������������������������������������������������������������������������� return Statements���������������������������������������������������������������������������������������������������������������������������������� Namespaces������������������������������������������������������������������������������������������������������������������������������������������� Names and Keywords����������������������������������������������������������������������������������������������������������������������������� Classes and Objects��������������������������������������������������������������������������������������������������������� Templates������������������������������������������������������������������������������������������������������������������������� Code Appearance and Programming Style����������������������������������������������������������������������� Creating an Executable�������������������������������������������������������������������������������������������������� 10 Procedural and Object-Oriented Programming�������������������������������������������������������������� 12 v www.allitebooks.com ■ Contents Representing Numbers��������������������������������������������������������������������������������������������������� 13 Binary Numbers������������������������������������������������������������������������������������������������������������������������������������ 13 Hexadecimal Numbers������������������������������������������������������������������������������������������������������������������������� 14 Negative Binary Numbers��������������������������������������������������������������������������������������������������������������������� 16 Octal Values������������������������������������������������������������������������������������������������������������������������������������������ 17 Bi-Endian and Little-Endian Systems��������������������������������������������������������������������������������������������������� 18 Floating-Point Numbers������������������������������������������������������������������������������������������������������������������������ 19 Representing Characters������������������������������������������������������������������������������������������������ 20 ASCII Codes������������������������������������������������������������������������������������������������������������������������������������������ 21 UCS and Unicode���������������������������������������������������������������������������������������������������������������������������������� 21 C++ Source Characters�������������������������������������������������������������������������������������������������� 22 Escape Sequences������������������������������������������������������������������������������������������������������������������������������� 22 Summary������������������������������������������������������������������������������������������������������������������������ 24 ■Chapter ■ 2: Introducing Fundamental Types of Data�������������������������������������������� 27 Variables, Data, and Data Types������������������������������������������������������������������������������������� 27 Defining Integer Variables�������������������������������������������������������������������������������������������������������������������� 28 Zero Initialization���������������������������������������������������������������������������������������������������������������������������������� 31 Defining Variables with Fixed Values���������������������������������������������������������������������������������������������������� 32 Integer Literals��������������������������������������������������������������������������������������������������������������� 32 Decimal Integer Literals����������������������������������������������������������������������������������������������������������������������� 32 Hexadecimal Literals���������������������������������������������������������������������������������������������������������������������������� 33 Octal Literals����������������������������������������������������������������������������������������������������������������������������������������� 34 Binary Literals�������������������������������������������������������������������������������������������������������������������������������������� 34 Calculations with Integers���������������������������������������������������������������������������������������������� 35 Compound Arithmetic Expressions������������������������������������������������������������������������������������������������������� 36 Assignment Operations�������������������������������������������������������������������������������������������������� 37 The op= Assignment Operators������������������������������������������������������������������������������������������������������������ 40 The sizeof Operator�������������������������������������������������������������������������������������������������������� 42 vi www.allitebooks.com ■ Contents Incrementing and Decrementing Integers���������������������������������������������������������������������� 42 Postfix Increment and Decrement Operations�������������������������������������������������������������������������������������� 43 Defining Floating-Point Variables����������������������������������������������������������������������������������� 44 Floating-Point Literals���������������������������������������������������������������������������������������������������� 45 Floating-Point Calculations�������������������������������������������������������������������������������������������� 46 Pitfalls��������������������������������������������������������������������������������������������������������������������������������������������������� 46 Invalid Floating-Point Results��������������������������������������������������������������������������������������������������������������� 47 Mathematical Functions����������������������������������������������������������������������������������������������������������������������� 48 Formatting Stream Output���������������������������������������������������������������������������������������������� 51 Mixed Expressions and Type Conversion������������������������������������������������������������������������ 53 Explicit Type Conversion������������������������������������������������������������������������������������������������� 54 Old-Style Casts������������������������������������������������������������������������������������������������������������������������������������� 56 Finding the Limits����������������������������������������������������������������������������������������������������������� 57 Finding Other Properties of Fundamental Types����������������������������������������������������������������������������������� 58 Working with Character Variables���������������������������������������������������������������������������������� 59 Working with Unicode Characters�������������������������������������������������������������������������������������������������������� 60 The auto Keyword���������������������������������������������������������������������������������������������������������� 61 Summary������������������������������������������������������������������������������������������������������������������������ 62 ■Chapter ■ 3: Working with Fundamental Data Types���������������������������������������������� 65 Operator Precedence and Associativity�������������������������������������������������������������������������� 65 Bitwise Operators����������������������������������������������������������������������������������������������������������� 67 The Bitwise Shift Operators������������������������������������������������������������������������������������������������������������������ 68 Logical Operations on Bit Patterns������������������������������������������������������������������������������������������������������� 71 Enumerated Data Types�������������������������������������������������������������������������������������������������� 77 Aliases for Data Types���������������������������������������������������������������������������������������������������� 80 The Lifetime of a Variable����������������������������������������������������������������������������������������������� 81 Global Variables�������������������������������������������������������������������������������������������������������������� 82 Summary������������������������������������������������������������������������������������������������������������������������ 85 vii www.allitebooks.com ■ Contents ■Chapter ■ 4: Making Decisions������������������������������������������������������������������������������� 89 Comparing Data Values�������������������������������������������������������������������������������������������������� 89 Applying the Comparison Operators����������������������������������������������������������������������������������������������������� 90 Comparing Floating-Point Values��������������������������������������������������������������������������������������������������������� 92 The if Statement������������������������������������������������������������������������������������������������������������� 92 Nested if Statements���������������������������������������������������������������������������������������������������������������������������� 96 Character Classification and Conversion���������������������������������������������������������������������������������������������� 97 The if-else Statement����������������������������������������������������������������������������������������������������� 99 Nested if-else Statements������������������������������������������������������������������������������������������������������������������ 101 Understanding Nested ifs������������������������������������������������������������������������������������������������������������������� 102 Logical Operators��������������������������������������������������������������������������������������������������������� 103 Logical AND���������������������������������������������������������������������������������������������������������������������������������������� 104 Logical OR������������������������������������������������������������������������������������������������������������������������������������������� 104 Logical Negation��������������������������������������������������������������������������������������������������������������������������������� 105 Combining Logical Operators������������������������������������������������������������������������������������������������������������� 105 Logical Operators on Integer Operands���������������������������������������������������������������������������������������������� 107 Logical Operators vs Bitwise Operators�������������������������������������������������������������������������������������������� 108 The Conditional Operator���������������������������������������������������������������������������������������������� 110 The switch Statement�������������������������������������������������������������������������������������������������� 112 Fallthrough����������������������������������������������������������������������������������������������������������������������������������������� 116 Statement Blocks and Variable Scope�������������������������������������������������������������������������� 118 Initialization Statements��������������������������������������������������������������������������������������������������������������������� 119 Summary���������������������������������������������������������������������������������������������������������������������� 120 ■Chapter ■ 5: Arrays and Loops����������������������������������������������������������������������������� 123 Arrays��������������������������������������������������������������������������������������������������������������������������� 123 Using an Array������������������������������������������������������������������������������������������������������������������������������������ 123 Understanding Loops��������������������������������������������������������������������������������������������������� 125 The for Loop����������������������������������������������������������������������������������������������������������������� 126 Avoiding Magic Numbers��������������������������������������������������������������������������������������������� 128 Defining the Array Size with the Braced Initializer������������������������������������������������������� 130 viii www.allitebooks.com ■ Contents Determining the Size of an Array��������������������������������������������������������������������������������� 130 Controlling a for Loop with Floating-Point Values�������������������������������������������������������� 132 More Complex for Loop Control Expressions ��������������������������������������������������������������� 135 The Comma Operator�������������������������������������������������������������������������������������������������������������������������� 136 The Range-Based for Loop������������������������������������������������������������������������������������������� 137 The while Loop������������������������������������������������������������������������������������������������������������� 138 The do-while Loop�������������������������������������������������������������������������������������������������������� 140 Nested Loops���������������������������������������������������������������������������������������������������������������� 142 Skipping Loop Iterations����������������������������������������������������������������������������������������������� 145 Breaking Out of a Loop������������������������������������������������������������������������������������������������� 146 Indefinite Loops���������������������������������������������������������������������������������������������������������������������������������� 146 Controlling a for Loop with Unsigned Integers������������������������������������������������������������� 150 Arrays of Characters����������������������������������������������������������������������������������������������������� 152 Multidimensional Arrays����������������������������������������������������������������������������������������������� 155 Initializing Multidimensional Arrays���������������������������������������������������������������������������������������������������� 158 Multidimensional Character Arrays���������������������������������������������������������������������������������������������������� 160 Allocating an Array at Runtime������������������������������������������������������������������������������������� 161 Alternatives to Using an Array�������������������������������������������������������������������������������������� 164 Using array Containers������������������������������������������������������������������������������������������������������������� 164 Using std::vector Containers�������������������������������������������������������������������������������������������������������� 169 Summary���������������������������������������������������������������������������������������������������������������������� 174 ■Chapter ■ 6: Pointers and References������������������������������������������������������������������ 177 What Is a Pointer?�������������������������������������������������������������������������������������������������������� 177 The Address-Of Operator���������������������������������������������������������������������������������������������� 180 The Indirection Operator����������������������������������������������������������������������������������������������� 181 Why Use Pointers?������������������������������������������������������������������������������������������������������� 183 Pointers to Type char���������������������������������������������������������������������������������������������������� 183 Arrays of Pointers������������������������������������������������������������������������������������������������������������������������������� 186 Constant Pointers and Pointers to Constants��������������������������������������������������������������� 188 ix www.allitebooks.com ■ Contents Pointers and Arrays������������������������������������������������������������������������������������������������������ 190 Pointer Arithmetic������������������������������������������������������������������������������������������������������������������������������� 191 Using Pointer Notation with an Array Name��������������������������������������������������������������������������������������� 193 Dynamic Memory Allocation����������������������������������������������������������������������������������������� 196 The Stack and the Free Store������������������������������������������������������������������������������������������������������������� 196 Using the new and delete Operators�������������������������������������������������������������������������������������������������� 197 Dynamic Allocation of Arrays�������������������������������������������������������������������������������������������������������������� 199 Member Selection Through a Pointer��������������������������������������������������������������������������� 203 Hazards of Dynamic Memory Allocation����������������������������������������������������������������������� 203 Dangling Pointers and Multiple Deallocations������������������������������������������������������������������������������������ 204 Allocation/Deallocation Mismatch������������������������������������������������������������������������������������������������������ 204 Memory Leaks������������������������������������������������������������������������������������������������������������������������������������ 204 Fragmentation of the Free Store��������������������������������������������������������������������������������������������������������� 205 Golden Rule of Dynamic Memory Allocation���������������������������������������������������������������� 206 Raw Pointers and Smart Pointers�������������������������������������������������������������������������������� 206 Using unique_ptr Pointers����������������������������������������������������������������������������������������������������������� 208 Using shared_ptr Pointers����������������������������������������������������������������������������������������������������������� 211 Understanding References������������������������������������������������������������������������������������������� 214 Defining References��������������������������������������������������������������������������������������������������������������������������� 214 Using a Reference Variable in a Range-Based for Loop��������������������������������������������������������������������� 216 Summary���������������������������������������������������������������������������������������������������������������������� 217 ■Chapter ■ 7: Working with Strings����������������������������������������������������������������������� 219 A Better Class of String������������������������������������������������������������������������������������������������ 219 Defining string Objects����������������������������������������������������������������������������������������������������������������������� 220 Operations with String Objects����������������������������������������������������������������������������������������������������������� 223 Accessing Characters in a String������������������������������������������������������������������������������������������������������� 228 Accessing Substrings������������������������������������������������������������������������������������������������������������������������� 230 Comparing Strings������������������������������������������������������������������������������������������������������������������������������ 230 Searching Strings������������������������������������������������������������������������������������������������������������������������������� 237 Modifying a String������������������������������������������������������������������������������������������������������������������������������ 243 std::string vs std::vector�������������������������������������������������������������������������������������������������������� 248 x www.allitebooks.com Chapter 19 ■ Containers and Algorithms Parallel Algorithms Some of the most notable additions to the C++17 Standard Library are parallel versions of most generic algorithms Virtually every computer today has multiple processing cores Even the most modest of phones has multiple processing cores these days By default, invoking any of the standard algorithms, however, will use only one of these cores All other cores risk sitting by idly, waiting until someone throws some work in their direction That is a real shame When processing large arrays or containers of data, these algorithms could run so much faster if they’d just divide the work among all available cores With C++17, doing this becomes easy All you have to do, for instance, to sort the fellowship in Ex19_15 in parallel is tell the algorithm to use the so-called parallel execution policy, like so: std::sort(std::execution::par, begin(names), end(names)); The std::execution::par constant is defined by the execution header, so you’ll have to include that first The header defines other execution policies as well, but we won’t discuss them here Naturally, with only nine elements, you are unlikely to notice any difference Of course, if Saruman or Sauron were to sort the names of their troops, then parallel execution would make a lot more sense Nearly every algorithm can be parallelized this way It costs you almost nothing, and the gains could be significant So, always keep this option in mind when processing larger data sets ■■Tip  The algorithm header also defines the for_each() algorithm, which you now could use to parallelize many regular range-based for loops Do take care, though, that each iteration of the loop can execute independently of the other, or you’ll run into data races Data races and other aspects of concurrent programming, however, are outside the scope of this book Summary This chapter provided you with a solid first introduction on three of the most important, most frequently used features of the Standard Library: containers, iterators, and algorithms Containers organize your data using various data structures, each with their strengths and limitations A typical container, and sequential containers in particular, not offer much functionality beyond adding, removing, and traversing elements More advanced operations to manipulate the data that is stored inside these containers are provided instead in the form of an impressive collection of generic, higher-order function templates, called algorithms Our goal here was never to make you an expert user of the various container and algorithm templates yet For that, considerably more pages are required than we had left for this book To actively start using the features that you learned about in this chapter, you’ll therefore need to regularly consult a Standard Library reference—one that lists all member functions of the various containers, as well as the many algorithm templates that exist (there are more than 100 of them in total!), and that specifies the precise semantics of all this powerful functionality Even the most seasoned C++ developer regularly needs guidance from a good reference book or website 756 Chapter 19 ■ Containers and Algorithms In this chapter, we therefore aimed to convey a broad bird’s-eye overview, focused on general principles, best practices, and common caveats to watch out for, with guidelines on choosing between the rich set of features that the Standard Library has to offer, typical use cases, and standard idioms In short, it contained everything that you cannot readily extract from a typical reference work The most important points covered in this chapter are the following: • Sequence containers store data in a straightforward user-determined linear order, one element after the other • Your go-to sequential container should be std::vector The practical use for the other sequential containers in real-life applications, and list, forward_list, and deque in particular, is typically limited • The three container adapters—std::stack, queue, and priority_queue— all encapsulate a sequential container, which they use to implement a limited set of operations that allow you to inject and later take out elements Their difference mostly lies in the order in which these elements come out again • Sets are duplicate-free containers and are good at determining whether they contain a given element • Maps uniquely associate keys with values and allow you to quickly retrieve a value given their keys • Both sets and maps come in two flavors: ordered and unordered The former are particularly interesting if you need a sorted view on your data as well; the latter have the potential to be more efficient but may come with the complexity of having to define an effective hash function first (we did not cover hash functions here, but you can read all about it in your favorite Standard Library reference) • You—and of course the Standard algorithms as well—can use iterators to enumerate the elements of any given container, without having to know how this data is actually physically organized • Iterators in C++ typically make heavy use of operator overloading in order to look and feel like pointers • The Standard Library offers more than a 100 different algorithms, most in the algorithms header We made sure that the ones you’ll likely use most often are covered either in the main text or in the following exercises • All algorithms operate on half-open ranges of iterators, and many accept a first-class callback function Mostly you’ll call an algorithm with a lambda expression if its default behavior does not suit you • Algorithms that retrieve a single element from a range (find(), find_if(), min_element(), max_element(), and so on) so by returning an iterator The end iterator of the range is then always used to denote “not found.” • Algorithms that produce multiple output elements (copy(), copy_if(), and so on) should normally always be used in conjunction with the std::back_inserter(), front_inserter(), and inserter() utilities provided by the iterator header • To remove multiple elements from sequence containers, you should use the remove-erase idiom • You can take advantage of the extensive multiprocessing capabilities of current hardware by passing the std::execution::par execution policy as the first argument to most algorithms 757 Chapter 19 ■ Containers and Algorithms EXERCISES The following exercises enable you to try what you’ve learned in this chapter If you get stuck, look back over the chapter for help If you’re still stuck after that, you can download the solutions from the Apress website (www.apress.com/book/download.html), but that really should be a last resort Exercise 19-1 In practice, we would never recommend you to implement your own linked list data structure to store Boxes in Truckload At the time it made perfect sense to practice nested classes, as well as working with pointers; but normally you should follow our advice from earlier in this chapter and simply use a vector instead (a polymorphic vector, to be precise—see Chapter 14) If you need a sequence container, a vector is almost always the way to go! Eliminate the linked list from the Truckload class of Exercise 17-1 according to this guideline Notice how you can now adhere to the rule of zero as well (see Chapter 18)? Exercise 19-2 Replace both instances of your self-defined Stack in Ex16_04A with an instance of std::stack Exercise 19-3 Rework your solution to Exercise 16-6 by replacing all instances of your SparseArray and linked list template types with standard containers Carefully think about which container types would be most appropriate! ■■Note If you want extra practice, you can the same for the solutions of Exercises 16-4 and 16-5 as well Exercise 19-4 Research the std::partition() algorithm and use it to reimplement the removeEvenNumbers() function of either Ex19_10 or Ex19_14 Exercise 19-5 Not all Standard Library algorithms are defined by the algorithms header Some are defined by the numeric header as well One such example is accumulate() Research this algorithm and use it to implement an algorithm-like function template that computes the average of a given iterator range Exercise your newly implemented template with a little test program Exercise 19-6 Another algorithm that is defined by the numeric header is the oddly named iota() algorithm, which you can use to fill a given range with values M, M+1, M+2, and so on Use it to rework the fillVector_1_to_N() function of Ex19_10 ■■Note The name of the iota() algorithm refers to the Greek letter iota, written ⍳ It is an homage to the classical programming language APL developed by Turing Award winner Kenneth E Iverson in the 1960s in his influential book A Programming Language (this title is where the acronym APL itself stems from) The APL programming language used mathematical symbols to name numeric functions, one of which was ⍳ In APL, ⍳3, for instance, would produce the array {1 3} 758 Chapter 19 ■ Containers and Algorithms Exercise 19-7 erase() and erase_if() are not the only algorithms for which the remove-erase idiom is applicable Another example is std::unique(), which is used to remove duplicates from a presorted range of elements Write a little program that fills a vector with a considerably large amount of random integers between and RAND_MAX, sorts this sequence, removes the duplicates, and then prints out the amount of remaining elements Exercise 19-8 Parallelize your solution to the previous exercise 759 Index „„         0-9/Symbols !, 105 !=, 89 #, 348 ##, 348 %, 36 & address-of operator, 697 bitwise operator, 71 capture default, 180, 713, 714 &&, 104 * indirection operator, 181 iterators, 737 multiplication, 35 +, 35 iterators, 738 ++ increment operator, 42 iterators, 737 ,, 136 -, 35 -decrement operator, 42 iterators, 737 /, 36 = capture default, 714 ==, 89 ?:, 110 [] arrays, 124 lambda initializer, 708, 711 map, 730, 732 sequence containers, 725 std::array, 164 std::string, 228 std::vector, 171 structured binding, 731 ^, 74 ||, 104 -> iterators, 738 lambda return type, 708 member access operator, 203 trailing return type, 329 bitwise operator, 68 overloading, 463 stream operator, 39 „„         A abort(), 592 abs(), 48 Absolute value, 48 Abstract classes, 561 Accessor function, 400 Access specifiers, 537 private, 386 protected, 497 public, 386 Access violation, 127 acos(), 48 Addition, 35 Address, 177 Address-of, 180 adjecent_find(), 751 Aggregation, 494 Algorithms, 747 © Ivor Horton and Peter Van Weert 2018 I Horton and P Van Weert, Beginning C++17, https://doi.org/10.1007/978-1-4842-3366-5 761 ■ INDEX , 322, 749 Alias function pointer type, 701 namespace, 362 type, 80 Alias template, 702 Alignment, 416 Alpha channel, 67 AND bitwise operator, 71 logical operators, 104 Anonymous function, 708 append(), 226 Argument function, 259 function template, 319 Arithmetic expressions, 36 Array characters, 152 index, 124 initialization, 124 multidimensional, 155, 201 size, 130 variable, 123 variable-length, 162 , 131, 164 array, 164, 722, 725 ASCII, 21, 59 as_const(), 621 asin(), 48 assert(), 371 Assertion runtime, 371 static, 373, 645 assign() string, 248 vector, 172 Assignment, 37 Assignment operator copy, 483, 623, 688 move, 670, 685, 688 Associative array, 730 Associativity, 65 at() array, 166 sequence containers, 725 string, 248 vector, 171 atan(), 48 atan2(), 49 auto in range-based for, 216 return type, 293, 328 variable type, 61 Automatic variable, 81 762 „„         B back() sequence containers, 725 string, 248 vector, 171 back_inserter(), 753 bad_alloc, 604, 607, 624 bad_cast, 604 bad_function_call, 718 bad_optional_access, 604 bad_typeid, 604 Balanced tree, 729 Base, 13 Base class, 383, 493 Base class pointer, 525 Base class specifier, 498 basic_string, 251 begin(), 737, 747 Behavior, 383 Bidirectional iterator, 738 Bi-endian, 18 Binary addition, 14 Binary literals, 35 Binary notation, 13 binary_search(), 751 Binding, 529 Bit, 13 bit_and, 706 bit_not, 706 bit_or, 706 Bitwise operators AND, 71 combining, 74 complement, 73 logical, 71, 74 OR, 72 shift, 68 XOR, 74 bit_xor, 706 Block, 6, 81, 118 bool, 90 boolalpha, 91 Booleans, 90 Braced initializer, 28, 61 break from loop, 146 from switch, 112 Byte, 16 „„         C C++11, C++14, C++17, ■ INDEX Callback function, 699, 710 Capitalize letters, 97 Capture-by-value, 711 Capture clause, 711 Capture default, 711 case, 112 Cast const_cast, 409, 622 C-style, 57 dynamic_cast, 548, 604 overloading, 482 static_cast, 54 Catastrophic cancellation, 20, 46 catch, 573 Catch-all, 593 cbegin(), 742, 747 , 98 ceil(), 48 cend(), 742, 747 char, 31, 59 Character arrays, 152 Character classification, 97 Character conversion, 97 Character literal, 23, 59 Characters ASCII, 21 escaping, 22 representation, 20 source, 22 Unicode, 21 char16_t, 60 char32_t, 60 cin, Class, 9, 379, 385 class class definition, 386 template parameter, 320 Class definition, 386 Class diagram, 492 Class template, 614 Class template argument deduction, 632 clear() containers, 724 set, 728 string, 248 vector, 172, 674 , 48 Code duplication, 620, 655 Coding style, 10 Combining logical operators, 105 Comments, 4, 640 compare(), 233 Comparing strings, 231, 233 Comparison operators, 90 Compiler, 10, 339 Composition, 494 Compound assignment, 40 Compound statement, Concatenation characters, 226 numbers, 227 strings, 224 string views, 282 Conditional operator, 110 conditional_t, 685 const functions, 405 objects, 404 overloading, 407 parameter, 270 pointer, 188, 270 variable, 32 const-and-back-again, 622 Constants, 32, 342 const_cast, 409, 622 const correctness, 406 constexpr, 374 Const iterator, 741 Constructor, 388 delegation, 397 inheritance, 508 Container adapter, 725, 739 Containers, 164, 542, 686, 692, 721 continue, 145 Conversion, 28, 53 copy(), 753 Copy-and-swap, 625, 688 Copy assignment operator, 483, 623, 688 Copy constructor, 398 copy_if(), 752 cos(), 48 count(), 728, 730 Coupling, 383 cout, 4, Covariance, 534 cplusplus, 365 Crash dump, 592 Crosscast, 548 , 592 c_str(), 220 , 219 „„         D Dangling pointer, 204, 624, 670 data() array, 168 sequence containers, 725 763 ■ INDEX data() (cont.) string, 220 string_view, 282 Data hiding, 381, 501, 725 Data member, 385 DATE_, 365 Debugging, 367 dec, 51 Decimal number, 13 Declaration, 338 decltype(), 329 decltype(auto), 330 Decrementing, 42 Deep copy, 670 default constructor, 391, 508 destructor, 424 switch case, 112 Default argument values, 283, 304, 539 Default constructor, 388 defaultfloat, 51 #define, 345 defined, 351 Definition, 338 Delegating constructor, 397 delete deallocation, 198 special member function, 673 delete[], 199 Dependent type, 656 , 723 deque, 723, 725 Dereference operator, 181 Derived class, 383, 492, 493 Destructor, 424 Diamond problem, 520 Digit grouping, 33 digits, 58 divides, 706 Division, 35 do-while, 140 domain_error, 603 Don’t Repeat Yourself (DRY), 620 double, 45 Double-ended queue, 723 Double precision, 20 Doubly linked list, 722 Downcast, 548 Dynamic array, 199 Dynamic binding, 530 dynamic_cast, 548, 604 Dynamic memory, 196 Dynamic resolution, 530 Dynamic type, 527 764 „„         E Early binding, 529 #elif, 344, 364 else, 99 #else, 344, 364 empty(), 724 Encapsulation, 380 end(), 737, 740, 747 Endianness, 18 endl, 4, enum, 78 Enumerations, 78 Equality operator, 89 equal_to, 706 erase() during iteration, 744 sequence containers, 725, 743, 754 set, 728 string, 247 #error, 344 Escape sequences, 22 Exception, 572 , 592, 603 Exception safety, 623, 685 execution::par, 756 Execution policy, 756 exit(), 592 _Exit(), 592 exp(), 48 explicit, 394, 454 Explicit template instantiation, 643 Exponent, 19 extern, 342 External functions, 339 External linkage, 339 External variables, 341 „„         F Fallthrough, 116 [[fallthrough]], 117 Field, 385 FILE_, 365 fill(), 165 final, 535 find() algorithm, 750 string, 237 find_end(), 751 find_first_of(), 240, 751 find_if(), 750 find_if_not(), 751 find_last_of(), 240 First-class function, 695 ■ INDEX First-in-first-out (FIFO), 725 fixed, 51 float, 45 Floating-point infinity, 47 literals, 45 loop control variables, 132 NaN, 47 numbers, 19 types, 45 floor(), 48 for loop, 126 range-based, 137, 216 for_each(), 756 Formatting, 51 Forward declaration, see Function prototype Forward iterator, 738 , 722 forward_list, 722, 725 Fraction, 19 Fragmentation, 205 Free store, 197 friend class, 413 class templates, 647 function, 411 front() queue, 726 sequence containers, 725 string, 248 vector, 171 front_inserter(), 753 function, 716 , 705, 716 Function argument, 259 Function declarations, 263 Function definitions, 258 Function header, 5, 259 Function object, 480, 703 Function overriding, 534 Function parameter, 259 Function pointers, 696 Function prototype, 263 Functions, 5, 258 Function signature, 260 Function template, 319 Function template specialization, 323 Functor, see Function object „„         G get() unique_ptr, 208 getline(), 154, 229 Getter, 401 Global namespace, 354 Global variable, 82, 342 greater, 706, 727, 729 greater_equal, 706 „„         H Has a relation, 494 has_include, 366 has_value(), 296 Hash map, 729 Hash table, 729 Header , 322, 749 , 131, 164 , 98 , 48 , 127 , 592 , 219 , 723 , 592, 603 , 722 , 705, 716 , 51 , 51 , 4, 51 , 753 , 47, 57 , 722 , 730 , 207 , 294 , 725, 727 , 727 , 249 , 725 , 603 , 370 , 219 , 281 , 557 , 374, 646, 685 , 730 , 729 , 233, 456, 622, 684, 731 , 169, 724, 742 Header file, 3, 11, 337, 352, 391 Heap, 197 hex, 51 Hexadecimal literals, 34 Hexadecimal numbers, 15 Hiding members, 512 Higher-order function, 699 765 ■ INDEX „„         I IDE, 11, 367 if else, 99 initialization statement, 119 statement, 92 #if, 344, 362 #ifdef, 351 #ifndef, 351 Implicit conversion, 53, 260, 279, 470 #include, 4, 10, 350 #include guard, 352 Incrementing, 42 Indefinite loop, 146 Indirection operator, 181 Infinite loop, 146 Infinity, 47, 58 infinity(), 58 Information hiding, 381 Inheritance, 383, 493 Inheriting constructors, 508 Initialization statement for, 127 if, 119 switch, 120 initializer_list, 61 inline functions, 297, 338, 393 variables, 338, 339, 355, 417 Input iterator, 739 Input/output, Input parameter, 275 insert() sequence containers, 725, 743 set, 728 string, 244 inserter(), 753 Instance class, 380, 385 class template, 614 function template, 319, 320 int, 31 Integer division, 36 Integer literals, 33 Integer variables, 28 Integrated development environment (IDE), 11, 367 Interface, 382, 564 Internal functions, 343, 360 Internal linkage, 339, 360 Internationalization, see Locale invalid_argument, 603 Invalidated iterator, 744 , 51 , 51 766 , 4, 51 isalnum(), 98, 100 isalpha(), 98, 114 Is a relation, 493 is_arithmetic_v, 374 is_assignable_v, 646 isblank(), 98 is_class_v, 646 iscntrl(), 98 is_copy_constructible_v, 646 is_default_constructible_v, 646 isdigit(), 98 is_enum, 374 is_floating_point_v, 374 isgraph(), 98 is_integral_v, 374 islower(), 98 is_nothrow_move_assignable_v, 685 is_null_pointer_v, 646 is_pointer_v, 646 isprint(), 98 ispunct(), 98 is_signed_v, 374 isspace(), 98 isupper(), 98 is_unsigned_v, 374 isxdigit(), 98 Iterator containers, 737 pattern, 442, 735 , 753 „„         J, K Justification, 51 „„         L Lambda capture clause, 711 Lambda expressions, 707 Lambda initializer, 708 Last-in first-out (LIFO), 725 Late binding, 530 left, 51 length(), 220, 252 length_error, 603 less, 705, 727, 729 less_equal, 706 Lexicographical comparison, 231, 233 Lifetime, 81 , 47, 57 #line, 344 LINE_, 365 Linkage, 339 ■ INDEX Linked list, 429, 650, 722 Linker, 10, 339 list, 722, 725 Literals binary, 35 Boolean, 90 character, 23, 59 digit grouping, 33 hexadecimal, 34 integer, 33 octal, 34 raw string, 252 string, 23 Little-endian, 18 Locale, 97 Local variable, 81 log(), 48 log10(), 48 logical_and, 706 logical_not, 706 Logical operators AND, 104 combining, 105 negation, 105 OR, 104 short-circuit evaluation, 108 XOR, 109 logical_or, 706 logic_error, 603 long, 31 long double, 45 long long, 31 Loop breaking, 146 do-while, 140 for, 126 indefinite, 146 overview, 125 range-based for, 137, 216 skipping iterations, 145 while, 138 Lowercase, 99 lowest(), 57 Lvalue, 662 „„         M Macro function-like, 346 identifier, 345 Magic numbers, 128 Magnitude, 16 main(), 3, 285 make_shared(), 214 make_unique() T, 208 T[], 209 Mangled names, 559 Mantissa, 19 , 730 map, 730 Mathematical functions, 48 max() algorithm, 322 numeric_limits, 57, 145 max_element(), 749 Member function, 380, 385 Member initializer list, 394 Member variable, 380, 385 , 207 Memory leak, 204 Metaprogramming, 685 Method, 385 Method chaining, 404, 485 min() algorithm, 322, 608 numeric_limits, 57 min_element(), 749 minmax_element(), 750 minus, 706 Mixin, 513 Modern C++, Modulus, 35 modulus, 706 move(), 672, 674, 675 Move-and-swap, 688 Move assignment operator, 670, 685, 688 Move constructor, 668, 685 move_if_no_except(), 684 Move-only types, 672 Move semantics, 661, 664 Multidimensional arrays, 155, 201 Multiple inheritance, 513 Multiplication, 35 multiplies, 706 multiset, 730 mutable lambda expression, 712 member function, 410 Mutual recursion, 305 „„         N Names, namespace, 354 Namespace alias, 362 Namespaces, 7, 42, 353 Narrow characters, 60 Narrowing conversion, 28 767 ■ INDEX NDEBUG, 372 negate, 706 Negation bitwise, 73 logical, 105 Nested class, 440 Nested class template, 649 Nested namespaces, 360 new, 197 new[], 199 Newline character, 22 noexcept, 595, 597, 683, 687 noexcept(), 597 Nonmember operator function, 453, 461, 469 Nontype template parameter class template, 634 function template, 331 Not-a-number (NaN), 47, 58 not_equal_to, 706 npos, 237 Null character, 152 nullopt, 296 nullptr, 178 Numbers binary, 13 endianness, 18 floating-point, 19 hexadecimal, 14 negative, 16 octal,17 See also Literals Numerical stability, 20, 46 numeric_limits, 57 „„         O Object, 9, 380, 385 Object file, 10 Object slicing, 588 See also Slicing Object-oriented programming, 12, 379 Observer, 701 oct, 51 Octal literals, 34 Octal numbers, 17 One Definition Rule (ODR), 338, 417 Operator associativity, 65 Operator overloading, 450, 458, 461, 469 Operator precedence, 65 Operators bitwise (see Bitwise operators) logical (see Logical operators) , 294 optional, 294, 604 OR bitwise operators, 72 logical operators, 104 768 out_of_range, 604, 607 Output iterator, 739 Output parameter, 275, 667 Overflow, 54 overflow_error, 603 Overloading, 462, 463 && and ||, 460 ++ and , 473 [], 474

Ngày đăng: 21/05/2018, 19:23

Từ khóa liên quan

Mục lục

  • Contents

  • About the Authors

  • About the Technical Reviewer

  • Introduction

  • Chapter 1: Basic Ideas

    • Modern C++

    • Standard Libraries

    • C++ Program Concepts

      • Source Files and Header Files

      • Comments and Whitespace

      • Preprocessing Directives and Standard Library Headers

      • Functions

      • Statements

      • Data Input and Output

      • return Statements

      • Namespaces

      • Names and Keywords

      • Classes and Objects

      • Templates

      • Code Appearance and Programming Style

      • Creating an Executable

      • Procedural and Object-Oriented Programming

Tài liệu cùng người dùng

Tài liệu liên quan