C++ Standard Library Quick Reference (Peter Van Weert, Marc Gregoire (auth.)) (Z-Library)

Author: Peter Van Weert, Marc Gregoire (auth.)

科学

No Description

📄 File Format: PDF
💾 File Size: 20.9 MB
10
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
THE E XPER T ’S VOICE® I N C+ + C++ Standard Library Quick Reference — Peter Van Weert Marc Gregoire
📄 Page 2
C++ Standard Library Quick Reference Peter Van Weert Marc Gregoire
📄 Page 3
C++ Standard Library Quick Reference Peter Van Weert Marc Gregoire Kessel-Lo, Belgium Meldert, Belgium ISBN-13 (pbk): 978-1-4842-1875-4 ISBN-13 (electronic): 978-1-4842-1876-1 DOI 10.1007/978-1-4842-1876-1 Library of Congress Control Number: 2016941348 Copyright © 2016 by Peter Van Weert and Marc Gregoire 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. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. 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: Welmoed Spahr Lead Editor: Steve Anglin Technical Reviewer: Bart Vandewoestyne Editorial Board: Steve Anglin, Pramila Balan, Louise Corrigan, Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing Coordinating Editor: Mark Powers Copy Editor: Tiffany Taylor Compositor: SPi Global Indexer: SPi Global Artist: SPi Global 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 rights@apress.com , or visit www.apress.com . Apress and friends of ED books 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 Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales . Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com/9781484218754 . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. Printed on acid-free paper
📄 Page 4
To my parents and my brother and his wife. Th eir support and patience helped me in fi nishing this book. —Marc Gregoire In loving memory of Jeroen. Your enthusiasm and courage will forever remain an inspiration to us all. —Peter Van Weert
📄 Page 5
(This page has no text content)
📄 Page 6
v Contents at a Glance About the Authors ............................................................................ xv About the Technical Reviewer ....................................................... xvii Introduction .................................................................................... xix ■Chapter 1: Numerics and Math ....................................................... 1 ■Chapter 2: General Utilities ........................................................... 23 ■Chapter 3: Containers ................................................................... 51 ■Chapter 4: Algorithms .................................................................. 81 ■Chapter 5: Stream I/O ................................................................. 101 ■Chapter 6: Characters and Strings ............................................. 125 ■Chapter 7: Concurrency .............................................................. 161 ■Chapter 8: Diagnostics ............................................................... 183 ■Appendix A: Standard Library Headers ...................................... 195 Index .............................................................................................. 201
📄 Page 7
(This page has no text content)
📄 Page 8
vii Contents About the Authors ............................................................................ xv About the Technical Reviewer ....................................................... xvii Introduction .................................................................................... xix ■Chapter 1: Numerics and Math ....................................................... 1 Common Mathematical Functions .............................................. <cmath> 1 Basic Functions ........................................................................................................ 1 Exponential and Logarithmic Functions ................................................................... 2 Power Functions ....................................................................................................... 2 Trigonometric and Hyperbolic Functions .................................................................. 2 Error and Gamma Functions ..................................................................................... 3 Integral Rounding of Floating-Point Numbers .......................................................... 3 Floating-Point Manipulation Functions ..................................................................... 3 Classifi cation and Comparison Functions ................................................................. 4 Error Handling ........................................................................................................... 5 Fixed-Width Integer Types .......................................................<cstdint> 5 Arithmetic Type Properties ...................................................... <limits> 5 Complex Numbers ..................................................................<complex> 8 Compile-Time Rational Numbers .............................................. <ratio> 9 Random Numbers ................................................................... <random> 10 Random Number Generators .................................................................................. 10 Random Number Distributions ............................................................................... 13
📄 Page 9
■ CONTENTS viii Numeric Arrays ....................................................................<valarray> 17 std::slice ........................................................................................................... 19 std::gslice ......................................................................................................... 20 std::mask_array ................................................................................................ 21 std::indirect_array ........................................................................................ 21 ■Chapter 2: General Utilities ........................................................... 23 Moving, Forwarding, Swapping .............................................<utility> 23 Moving .................................................................................................................... 23 Forwarding ............................................................................................................. 25 Swapping ................................................................................................................ 26 Pairs and Tuples .................................................................................... 26 Pairs.................................................................................................... <utility> 26 Tuples ..................................................................................................... <tuple> 27 Relational Operators ..............................................................<utility> 28 Smart Pointers ....................................................................... <memory> 28 Exclusive-Ownership Pointers ................................................................................ 29 Shared-Ownership Pointers.................................................................................... 31 Function Objects ............................................................. <functional> 33 Reference Wrappers ............................................................................................... 34 Predefi ned Functors ............................................................................................... 34 Generic Function Wrappers .................................................................................... 35 Binding Function Arguments .................................................................................. 36 Functors for Class Members ................................................................................... 37 Initializer Lists ....................................................... <initializer_list> 39 Date and Time Utilities ........................................................... <chrono> 39 Durations ................................................................................................................ 40 Time Points ............................................................................................................. 41 Clocks ..................................................................................................................... 41 C-style Date and Time Utilities ............................................................... <ctime> 42
📄 Page 10
■ CONTENTS ix C-Style File Utilities ................................................................ <cstdio> 45 Type Utilities .......................................................................................... 45 Runtime Type Identifi cation ..................................... <typeinfo>, <typeindex> 45 Type Traits ...................................................................................<type_traits> 46 ■Chapter 3: Containers ................................................................... 51 Iterators ...............................................................................<iterator> 51 Iterator Tags ............................................................................................................ 52 Non-Member Functions to Get Iterators ................................................................. 53 Non-Member Operations on Iterators ..................................................................... 54 Sequential Containers ........................................................................... 54 std::vector ....................................................................................... <vector> 54 std::deque ........................................................................................... <deque> 60 std::array ........................................................................................... <array> 60 std::list and std::forward_list ......................<list>, <forward_list> 61 Sequential Containers Reference ........................................................................... 63 std::bitset ....................................................................................... <bitset> 66 Container Adaptors ................................................................................ 67 std::queue ........................................................................................... <queue> 68 std::priority_queue ........................................................................ <queue> 68 std::stack ........................................................................................... <stack> 69 Example .................................................................................................................. 69 Reference ............................................................................................................... 70 Ordered Associative Containers ............................................................ 71 std::map and std::multimap .................................................................<map> 71 std::set and std::multiset .................................................................<set> 72 Searching ............................................................................................................... 72 Order of Elements ................................................................................................... 73 Complexity .............................................................................................................. 73 Reference ............................................................................................................... 73
📄 Page 11
■ CONTENTS x Unordered Associative Containers .... <unordered_map>, <unordered_set> 75 Hash Map ................................................................................................................ 76 Template Type Parameters ..................................................................................... 76 Hash Functions ....................................................................................................... 76 Complexity .............................................................................................................. 77 Reference ............................................................................................................... 77 Allocators .............................................................................................. 79 ■Chapter 4: Algorithms .................................................................. 81 Input and Output Iterators ..................................................................... 81 Algorithms ......................................................................... <algorithm> 82 Terminology ............................................................................................................ 82 General Guidelines .................................................................................................. 82 Applying a Function on a Range ............................................................................. 83 Checking for the Presence of Elements .................................................................. 84 Finding Elements .................................................................................................... 84 Binary Search ......................................................................................................... 85 Subsequence Search .............................................................................................. 86 Min/Max .................................................................................................................. 87 Sequence Comparison ............................................................................................ 88 Copy, Move, Swap ................................................................................................... 88 Generating Sequences ............................................................................................ 89 Removing and Replacing ........................................................................................ 90 Reversing and Rotating .......................................................................................... 91 Partitioning ............................................................................................................. 92 Sorting .................................................................................................................... 93 Shuffl ing ................................................................................................................. 94 Operations on Sorted Ranges ................................................................................. 95
📄 Page 12
■ CONTENTS xi Permutation ............................................................................................................ 96 Heaps ...................................................................................................................... 97 Numeric Algorithms ............................................................................ <numeric> 98 Iterator Adaptors .................................................................<iterator> 99 ■Chapter 5: Stream I/O ................................................................. 101 Input and Output with Streams ........................................................... 101 Helper Types ..............................................................................................<ios> 102 std::ios_base .......................................................................................<ios> 103 I/O Manipulators ..................................................................<ios>, <iomanip> 105 Example ................................................................................................................ 106 std::ios ..................................................................................................<ios> 106 std::ostream................................................................................. <ostream> 108 std::istream................................................................................. <istream> 110 std::iostream .............................................................................. <istream> 112 String Streams ....................................................................<sstream> 112 Example ................................................................................................................ 113 File Streams ........................................................................<fstream> 113 Example ................................................................................................................ 114 operator<< and >> for Custom Types ............................................... 115 Stream Iterators ...............................................................<iterator> 115 std::ostream_iterator .................................................................................. 115 std::istream_iterator .................................................................................. 116 Stream Buffers ............................................................... <streambuf> 117 C-Style Output and Input ...................................................... <cstdio> 117 std::printf() Family ....................................................................................... 118 std::scanf() Family ......................................................................................... 122
📄 Page 13
■ CONTENTS xii ■Chapter 6: Characters and Strings ............................................. 125 Strings .................................................................................. <string> 125 Searching in Strings ............................................................................................. 126 Modifying Strings ................................................................................................. 127 Constructing Strings ............................................................................................. 128 String Length ........................................................................................................ 128 Copying (Sub)Strings ............................................................................................ 128 Comparing Strings ................................................................................................ 129 String Conversions ............................................................................................... 129 Character Classifi cation ...................................... <cctype>, <cwctype> 130 Character-Encoding Conversion ..........................<locale>, <codecvt> 131 Localization .......................................................................... <locale> 134 Locale Names ....................................................................................................... 134 The Global Locale ................................................................................................. 135 Basic std::locale Members ............................................................................. 136 Locale Facets ........................................................................................................ 136 Combining and Customizing Locales .................................................................... 145 C Locales .......................................................................................... <clocale> 147 Regular Expressions ............................................................... <regex> 148 The ECMAScript Regular Expression Grammar .................................................... 149 Regular Expression Objects .................................................................................. 153 Matching and Searching Patterns ........................................................................ 155 Match Iterators ..................................................................................................... 158 Replacing Patterns ............................................................................................... 159
📄 Page 14
■ CONTENTS xiii ■Chapter 7: Concurrency .............................................................. 161 Threads ................................................................................ <thread> 161 Launching a New Thread ...................................................................................... 161 A Thread’s Lifetime ............................................................................................... 162 Thread Identifi ers ................................................................................................. 162 Utility Functions .................................................................................................... 163 Exceptions ............................................................................................................ 163 Futures ................................................................................. <future> 164 Return Objects ...................................................................................................... 164 Providers............................................................................................................... 165 Exceptions ............................................................................................................ 167 Mutual Exclusion .................................................................... <mutex> 168 Mutexes and Locks ............................................................................................... 168 Mutex Types .......................................................................................................... 170 Lock Types ............................................................................................................ 171 Locking Multiple Mutexes ..................................................................................... 172 Exceptions ............................................................................................................ 173 Calling a Function Once ........................................................................ <mutex> 173 Condition Variables ...........................................<condition_variable> 174 Waiting for a Condition ......................................................................................... 174 Notifi cation ........................................................................................................... 175 Exceptions ............................................................................................................ 176 Synchronization ................................................................................... 176 Atomic Operations ................................................................ <atomic> 178 Atomic Variables ................................................................................................... 178 Atomic Flags ......................................................................................................... 181 Nonmember Functions ......................................................................................... 181 Fences .................................................................................................................. 182
📄 Page 15
■ CONTENTS xiv ■Chapter 8: Diagnostics ............................................................... 183 Assertions ...........................................................................<cassert> 183 Exceptions ...................................................<exception>, <stdexcept> 184 Exception Pointers ........................................................... <exception> 184 Nested Exceptions ........................................................... <exception> 186 System Errors .............................................................<system_error> 187 std::error_category ...................................................................................... 188 std::error_code .............................................................................................. 188 std::error_condition .................................................................................... 189 C Error Numbers ................................................................... <cerrno> 190 Failure Handling .............................................................. <exception> 190 std::uncaught_exception() ......................................................................... 190 std::terminate() ............................................................................................ 191 std::unexpected() .......................................................................................... 191 ■Appendix A: Standard Library Headers ...................................... 195 Numerics and Math (Chapter 1) .......................................................... 195 General Utilities (Chapter 2) ................................................................ 196 Containers (Chapter 3) ........................................................................ 197 Algorithms (Chapter 4) ........................................................................ 197 Stream I/O (Chapter 5) ......................................................................... 198 Characters and Strings (Chapter 6) ..................................................... 199 Concurrency (Chapter 7) ...................................................................... 199 Diagnostics (Chapter 8) ....................................................................... 200 The C Standard Library ........................................................................ 200 Index .............................................................................................. 201
📄 Page 16
xv About the Authors Peter Van Weert is a Belgian software engineer whose main interest and expertise are programming languages, algorithms, and data structures. He received his master’s of science in computer science summa cum laude with congratulations of the Board of Examiners from the University of Leuven. In 2010, he completed his PhD thesis on the design and efficient compilation of rule-based programming languages at the research group for declarative programming languages and artificial intelligence of the same university. During his doctoral studies, he was a teaching assistant for object-oriented programming (Java), software analysis and design, and declarative programming. After graduating, Peter joined Nikon Metrology to work on large-scale, industrial application software in the area of 3D laser scanning and point cloud inspection. At Nikon, he has mastered C++ and refactoring and debugging of very large code bases and has gained further proficiency in all aspects of the software development process, including the analysis of functional and technical requirements, and agile and scrum- based project and team management. In his spare time, he has co-authored two award-winning Windows 8 apps, and he is a regular speaker at and board member of the Belgian C++ Users Group. Marc Gregoire is a software engineer from Belgium. He graduated from the University of Leuven, Belgium, with a degree in “Burgerlijk ingenieur in de computer wetenschappen” (equivalent to a master’s of science in engineering in computer science). The year after, he received the cum laude degree of master’s in artificial intelligence at the same university. After his studies, Marc started working for a software consultancy company called Ordina Belgium. As a consultant, he worked for Siemens and Nokia Siemens Networks on critical 2G and 3G software running on Solaris for telecom operators. This required working in international teams stretching from South America and USA to EMEA and Asia. Now, Marc is working for Nikon Metrology on industrial 3D laser scanning software.
📄 Page 17
■ ABOUT THE AUTHORS xvi His main expertise is C/C++, specifically Microsoft VC++ and the MFC framework. He has experience in developing C++ programs running 24/7 on Windows and Linux platforms: for example, KNX/EIB home automation software. In addition to C/C++, Marc also likes C# and uses PHP for creating web pages. Since April 2007, he has received the yearly Microsoft MVP (Most Valuable Professional) award for his Visual C++ expertise. Marc is the founder of the Belgian C++ Users Group ( www.becpp.org ), author of Professional C++ , and a member on the CodeGuru forum (as Marc G). He maintains a blog at www.nuonsoft.com/blog/ .
📄 Page 18
xvii About the Technical Reviewer Bart Vandewoestyne is an enthusiastic, solo-parenting software engineer living in Belgium. After obtaining his master’s degree from the Computer Science department at the University of Leuven, he worked as a researcher in the numerical analysis and applied mathematics section of that same university. He successfully completed his PhD in 2008. Three years of postdoctoral work later, Bart left the academic world for industry. He now works as a senior development software engineer for Esterline Belgium, where he develops and maintains software for professional flight-simulator alignment. Bart enjoys reading about and exploring all aspects of C++ software development. In his spare time, and when he’s away from his keyboard, he enjoys running, swimming, paragliding, and spending quality time with his now 6-year-old son Jenne. He wants the world to know how much he cares about Jenne, and he hopes that his child will also transform his passion into his profession.
📄 Page 19
(This page has no text content)
📄 Page 20
xix Introd uction The C++ Standard Library The C++ Standard Library is a collection of essential classes and functions used by millions of C++ programmers on a daily basis. Being part of the ISO Standard of the C++ Programming Language, an implementation is distributed with virtually every C++ compiler. Code written with the C++ Standard Library is therefore portable across compilers and target platforms. The Library is more than 20 years old. Its initial versions were heavily inspired by a (then proprietary) C++ library called the Standard Template Library (STL) , so much so that many still incorrectly refer to the Standard Library as “the STL.” The STL library pioneered generic programming with templated data structures called containers and algorithms , glued together with the concept of iterators . Most of this work was adapted by the C++ standardization committee, but nevertheless neither library is a true superset of the other. The C++ Standard Library today is much more than the STL containers and algorithms. For decades, it has featured STL-like string classes, extensive localization facilities, and a stream-based I/O library, as well as all headers of the C Standard Library. In recent years, the C++11 and C++14 editions of the ISO standard have added, among other things, hash map containers, generic smart pointers, a versatile random-number- generation framework, a powerful regular expression library, more expressive utilities for function-style programming, type traits for template metaprogramming, and a portable concurrency library featuring threads, mutexes, condition variables, and atomic variables. Many of these libraries are based on Boost, a collection of open-source C++ libraries. And this is just the beginning: the C++ community has rarely been as active and alive as in the past few years. The next version of the Standard, tentatively called C++17, is expected to add even more essential classes and functions. Why This Book? Needless to say, it is hard to know and remember all the possibilities, details, and intricacies of the vast and growing C++ Standard Library. This handy reference guide offers a condensed, well-structured summary of all essential aspects of the C++ Standard Library and is therefore indispensable to any C++ programmer. You could consult the Standard itself, but it is written in a very detailed, technical style and is primarily targeted at Library implementors. Moreover, it is very long: the C++ Standard Library chapters alone are over 750 pages in length, and those on the
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List