C++17 Standard Library Quick Reference: A Pocket Guide to Data Structures, Algorithms, and Functions
By Peter Van Weert and Marc Gregoire
()
About this ebook
It is hard to remember all the possibilities, details, and intricacies of the vast and growing Standard Library. This handy reference guide is therefore indispensable to any C++ programmer. It offers a condensed, well-structured summary of all essential aspects of the C++ Standard Library. No page-long, repetitive examples or obscure, rarely used features. Instead, everything you need to know and watch out for in practice is outlined in a compact, to-the-point style, interspersed with practical tips and well-chosen, clarifying examples.
This new edition is updated to include all Standard Library changes in C++17, including the new vocabulary types std::string_view, any, optional, and variant; parallel algorithms; the file system library; specialized mathematical functions; and more.
What You Will Learn
- Gain the essentials that the C++ Standard Library has to offer
- Use containers to efficiently store and retrieve your data
- Inspect and manipulate your data with algorithms
- See how lambda expressions allow for elegant use of algorithms
- Discover what the standard string class provides and how to use it
- Write localized applications
- Work with file and stream-based I/O
- Prevent memory leaks with smart pointers
- Write safe and efficient multi-threaded code using the threading libraries
Who This Book Is For
All C++ programmers, irrespective of their proficiency with the language or the Standard Library. A secondary audience is developers who are new to C++, but not new to programming, and who want to learn more about the C++ Standard Library in a quick, condensed manner.
Read more from Peter Van Weert
Beginning C++20: From Novice to Professional Rating: 0 out of 5 stars0 ratingsBeginning C++17: From Novice to Professional Rating: 0 out of 5 stars0 ratings
Related to C++17 Standard Library Quick Reference
Related ebooks
Advanced Python Development: Using Powerful Language Features in Real-World Applications Rating: 0 out of 5 stars0 ratingsPro C# 9 with .NET 5: Foundational Principles and Practices in Programming Rating: 0 out of 5 stars0 ratingsOptions and Derivatives Programming in C++20: Algorithms and Programming Techniques for the Financial Industry Rating: 0 out of 5 stars0 ratingsComputer Practices Using C++ Rating: 0 out of 5 stars0 ratingsMicrosoft Visual C++ Windows Applications by Example Rating: 4 out of 5 stars4/5Design Patterns in C#: A Hands-on Guide with Real-world Examples Rating: 0 out of 5 stars0 ratingsShared Memory Application Programming: Concepts and Strategies in Multicore Application Programming Rating: 0 out of 5 stars0 ratingsClean C++20: Sustainable Software Development Patterns and Best Practices Rating: 0 out of 5 stars0 ratingsLearn C++ for Game Development Rating: 0 out of 5 stars0 ratingsSoftware Engineer's Reference Book Rating: 0 out of 5 stars0 ratingsC++17 Quick Syntax Reference: A Pocket Guide to the Language, APIs and Library Rating: 0 out of 5 stars0 ratingsProfessional Python Rating: 0 out of 5 stars0 ratingsPattern-Oriented Software Architecture, On Patterns and Pattern Languages Rating: 5 out of 5 stars5/5Embedded Computing: A VLIW Approach to Architecture, Compilers and Tools Rating: 0 out of 5 stars0 ratingsMastering C++ Network Automation Rating: 0 out of 5 stars0 ratingsDigital Computer Structure and Design Rating: 0 out of 5 stars0 ratingsModern C for Absolute Beginners: A Friendly Introduction to the C Programming Language Rating: 0 out of 5 stars0 ratings.NET DevOps for Azure: A Developer's Guide to DevOps Architecture the Right Way Rating: 0 out of 5 stars0 ratingsPatterns for Parallel Software Design Rating: 0 out of 5 stars0 ratingsPractical Parallel Programming Rating: 0 out of 5 stars0 ratingsLighttpd Rating: 4 out of 5 stars4/5Microsoft SharePoint 2013 Disaster Recovery Guide Rating: 0 out of 5 stars0 ratingsLisp (programming language) Complete Self-Assessment Guide Rating: 1 out of 5 stars1/5Database Tuning: Principles, Experiments, and Troubleshooting Techniques Rating: 4 out of 5 stars4/5Processor Description Languages Rating: 5 out of 5 stars5/5Schematron: A language for validating XML Rating: 0 out of 5 stars0 ratingsAn Introduction to Computational Fluid Mechanics by Example Rating: 5 out of 5 stars5/5Computer Systems Performance Evaluation and Prediction Rating: 4 out of 5 stars4/5CUDA Application Design and Development Rating: 0 out of 5 stars0 ratingsAn Introduction to Data Base Design Rating: 0 out of 5 stars0 ratings
Programming For You
Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsLearn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Photoshop For Beginners: Learn Adobe Photoshop cs5 Basics With Tutorials Rating: 0 out of 5 stars0 ratingsMastering Windows PowerShell Scripting Rating: 4 out of 5 stars4/5The Absolute Beginner's Guide to Binary, Hex, Bits, and Bytes! How to Master Your Computer's Love Language Rating: 5 out of 5 stars5/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5Hacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Problem Solving in C and Python: Programming Exercises and Solutions, Part 1 Rating: 5 out of 5 stars5/5Programming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratings
Reviews for C++17 Standard Library Quick Reference
0 ratings0 reviews
Book preview
C++17 Standard Library Quick Reference - Peter Van Weert
© Peter Van Weert and Marc Gregoire 2019
Peter Van Weert and Marc GregoireC++17 Standard Library Quick Referencehttps://doi.org/10.1007/978-1-4842-4923-9_1
1. Numerics and Math
Peter Van Weert¹ and Marc Gregoire²
(1)
Kessel-Lo, Belgium
(2)
Meldert, Belgium
Common Mathematical Functions
The
If all arguments are float, the return type is float as well. Analogous for double and long double inputs.
If mixed types or integers are passed, these numbers are converted to double, and a double is returned as well. If one of the inputs is a long double, long double is used instead.
Basic Functions
Exponential and Logarithmic Functions
Power Functions
Trigonometric and Hyperbolic Functions
The lesser-known trigonometric function atan2() is available as well. You use it to compute the angle between a vector (x, y) and the positive X axis. atan2(y, x) is similar to atan(y / x) except that its result correctly reflects the quadrant the vector is in (and that it also works if x is zero). Essentially, by dividing y by x in atan(y / x), one loses information regarding the sign of x and y.
Integral Rounding of Floating-Point Numbers
Floating-Point Manipulation Functions
Classification and Comparison Functions
gcd/lcm ../images/417649_2_En_1_Chapter/417649_2_En_1_Figc_HTML.gif
The functions gcd() and lcm() compute the greatest common divisor and least common multiple, respectively. They are defined as follows:
template
constexpr std::common_type_t
template
constexpr std::common_type_t
Both M and N must be integer types. As explained in Chapter 2, std::common_type_t
If N and M are equal, their common type is that same type as well.
If N and M are both smaller than int, their common type is int.
If the size of N and M differs, their common type is the largest type.
Otherwise, the common type is the one that is unsigned.
Error Handling
The mathematical functions from
MATH_ERRNO: Use the global errno variable (see Chapter 8).
MATH_ERREXCEPT: Use the floating-point environment,
Special Mathematical Functions ../images/417649_2_En_1_Chapter/417649_2_En_1_Figd_HTML.gif
C++17 adds a collection of specialized mathematical functions. All of these are available in multiple overloads. In the following table, the functions without an asterisk always return a double. For the functions marked with an asterisk, the return type is always double, unless one of its arguments is a long double, then the return type is long double as well.
Additionally, there are two extra versions of each function with a postfix f or l. These additional functions accept floats and return a float (f postfix), or accept long doubles and return a long double (l postfix). For example, assoc_laguerre(), assoc_laguerref(), and assoc_laguerrel().
Explaining all the details of these mathematical functions falls outside the scope of this book. The following table just shows the mathematical formula for each function. Please consult a mathematical reference for more details.
Note
At the time of writing, libc++, the implementation that ships with the Clang compiler, has not implemented these special mathematical functions yet.
Bessel Functions
Polynomials
Elliptic Integrals
Exponential Integrals
Error Functions
Gamma Functions
Beta Functions
Zeta Functions
Minimum, Maximum, and Clamping
The Standard Library provides a set of functions related to finding the minimum and maximum of two or more values. In the following function definitions, T is the element type, and the optional Compare parameter is a function object to be used to compare elements. T can be any type, not just a fundamental type. If no Compare function object is specified, operator< is used. The function object accepts two parameters and returns true if the first argument is less than the second, false otherwise. The ordering imposed must be a strict weak ordering, just as with the default operator<:
constexpr const T& min(const T& x, const T& y[, Compare comp])
constexpr const T& max(const T& x, const T& y[, Compare comp])
Returns a reference to the minimum or maximum of two values, or the first value if they are equal.
constexpr T min(initializer_list
constexpr T max(initializer_list
Returns a copy of the minimum or maximum value in a given initializer_list (discussed in Chapter 2), or a copy of the leftmost minimum or maximum if there are several elements equal to this extreme. Allows expressions of the form std::min({ x, y, z }) to quickly determine the extreme of a limited number of elements.
constexpr pair
minmax(const T& x, const T& y[, Compare comp])
Returns a pair containing references to the minimum and maximum of two values, in that order. If x and y are equal, pair(x, y) is returned.
constexpr pair
Returns a pair containing a copy of the minimum and maximum values in an initializer_list, in that order. If several elements are equal to the minimum, then a copy of the leftmost one is returned; if several are equal to the maximum, a copy of the rightmost is returned.
C++17 also adds the following std::clamp() function which can be used to clamp, or bound, a given value to a given range ../images/417649_2_En_1_Chapter/417649_2_En_1_Fige_HTML.gif :
constexpr const T&
clamp(const T& value, const T& low, const T& high[, Compare comp])
This function returns the following:
A reference to low if value < low
A reference to value if low ≤ value ≤ high
A reference to high if high < value
Fixed-Width Integer Types
The
std::(u)intX_t, an (unsigned) integer of exactly X bits (X= 8, 16, 32, or 64). Present only if supported by the target platform.
std::(u)int_leastX_t, the smallest (unsigned) integer type of at least X bits (X= 8, 16, 32, or 64).
std::(u)int_fastX_t, the fastest (unsigned) integer type of at least X bits (X= 8, 16, 32, or 64).
std::(u)intmax_t, the largest supported (unsigned) integer type.
std::(u)intptr_t, (unsigned) integer type large enough to hold a pointer. These type aliases are optional.
The header further defines macros for the minimum and maximum values of these (and some other) types, for instance, INT_FAST_8_MIN and INT_FAST_8_MAX for std::int_fast8_t. The standard C++ way of obtaining these values though is with the facilities of
Arithmetic Type Properties
The std::numeric_limits
The following members are relevant only for floating-point types. For integer types, they always equal or return zero:
Complex Numbers
The std::complex
All expected operators are available: +, -, *, /, +=, -=, *=, /=, =, ==, and !=, including overloads with a floating-point operand (which is then treated as a complex number with a zero imaginary part), and the >> and << operators for interaction with the streams of Chapter 5.
The std::literals::complex_literals namespace defines convenient literal operators for creating complex
The header furthermore defines the complex equivalents of several of the basic math functions seen earlier, that is, pow(), sqrt(), exp(), log(), and log10(), as well as all trigonometric and hyperbolic functions, that is, sin(), cos(), tan(), asin(), acos(), atan(), sinh(), cosh(), tanh(), asinh(), acosh(), and atanh().
Besides these, the following complex-specific non-member functions exist:
Compile-Time Rational Numbers
The std::ratio
Convenience type aliases exist for all standard SI ratios: std::kilo, for instance, is defined as ratio<1000>, and std::centi as ratio<1,100>. The full list is atto (10−18), femto (10−15), pico (10−12), nano (10−9), micro (10−6), milli (10−3), centi (10−2), deci (10−1), deca (10¹), hecto (10²), kilo (10³), mega (10⁶), giga (10⁹), tera (10¹²), peta (10¹⁵), and exa (10¹⁸), and for platforms with an integer type that is wider than 64 bit yocto (10−24), zepto (10−21), zetta (10²¹), and yotta (10²⁴).
All ratio types define two static members: num and den, containing the numerator and denominator of the rational number, but after normalization. The ratio’s type member equals the ratio type of this normalized rational number.
Arithmetic operations with ratios are possible, but it is again at the type level: the std::ratio_add template, for instance, takes two ratio types as template arguments and evaluates to the type that corresponds to the sum of these rational numbers. The ratio_subtract, ratio_multiply, and ratio_divide templates are analogous. To compare two ratio types, similar ratio_xxx templates are provided with xxx either equal, not_equal, less, less_equal, greater, or greater_equal.
The following example clarifies ratio arithmetic (