Modern C++ Programming Cookbook
eBook - ePub

Modern C++ Programming Cookbook

Master C++ core language and standard library features, with over 100 recipes, updated to C++20, 2nd Edition

Marius Bancila

Buch teilen
  1. 750 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Modern C++ Programming Cookbook

Master C++ core language and standard library features, with over 100 recipes, updated to C++20, 2nd Edition

Marius Bancila

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

A pragmatic recipe book for acquiring a comprehensive understanding of the complexities and core fundamentals of C++ programming

Key Features

  • Explore the latest language and library features of C++20 such as modules, coroutines, concepts, and ranges
  • Shed new light on the core concepts in C++ programming, including functions, algorithms, threading, and concurrency, through practical self-contained recipes
  • Leverage C++ features like smart pointers, move semantics, constexpr, and more for increased robustness and performance

Book Description

C++ has come a long way to be one of the most widely used general-purpose languages that is fast, efficient, and high-performance at its core.

The updated second edition of Modern C++ Programming Cookbook addresses the latest features of C++20, such as modules, concepts, coroutines, and the many additions to the standard library, including ranges and text formatting. The book is organized in the form of practical recipes covering a wide range of problems faced by modern developers.

The book also delves into the details of all the core concepts in modern C++ programming, such as functions and classes, iterators and algorithms, streams and the file system, threading and concurrency, smart pointers and move semantics, and many others. It goes into the performance aspects of programming in depth, teaching developers how to write fast and lean code with the help of best practices.

Furthermore, the book explores useful patterns and delves into the implementation of many idioms, including pimpl, named parameter, and attorney-client, teaching techniques such as avoiding repetition with the factory pattern. There is also a chapter dedicated to unit testing, where you are introduced to three of the most widely used libraries for C++: Boost.Test, Google Test, and Catch2.

By the end of the book, you will be able to effectively leverage the features and techniques of C++11/14/17/20 programming to enhance the performance, scalability, and efficiency of your applications.

What you will learn

  • Understand the new C++20 language and library features and the problems they solve
  • Become skilled at using the standard support for threading and concurrency for daily tasks
  • Leverage the standard library and work with containers, algorithms, and iterators
  • Solve text searching and replacement problems using regular expressions
  • Work with different types of strings and learn the various aspects of compilation
  • Take advantage of the file system library to work with files and directories
  • Implement various useful patterns and idioms
  • Explore the widely used testing frameworks for C++

Who this book is for

The book is designed for entry- or medium-level C++ programmers who have a basic knowledge of C++ and want to master the language and become prolific modern C++ developers. Experienced C++ programmers can leverage this book to strengthen their command of C++ and find a good reference to many language and library features of C++11/14/17/20.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Modern C++ Programming Cookbook als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Modern C++ Programming Cookbook von Marius Bancila im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Computer Science & Programming in C++. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2020
ISBN
9781800206205

2

Working with Numbers and Strings

Numbers and strings are the fundamental types of any programming language; all other types are based on or composed of these ones. Developers are confronted all the time with tasks such as converting between numbers and strings, parsing and formatting strings, and generating random numbers. This chapter is focused on providing useful recipes for these common tasks using modern C++ language and library features.
The recipes included in this chapter are as follows:
  • Converting between numeric and string types
  • Limits and other properties of numeric types
  • Generating pseudo-random numbers
  • Initializing all the bits of the internal state of a pseudo-random number generator
  • Creating cooked user-defined literals
  • Creating raw, user-defined literals
  • Using raw string literals to avoid escaping characters
  • Creating a library of string helpers
  • Verifying the format of a string using regular expressions
  • Parsing the content of a string using regular expressions
  • Replacing the content of a string using regular expressions
  • Using std::string_view instead of constant string references
  • Formatting text with std::format
  • Using std::format with user-defined types
Let's start this chapter by looking at a very common problem developers face on a daily basis, which is converting between numeric and string types.

Converting between numeric and string types

Converting between number and string types is a ubiquitous operation. Prior to C++11, there was little support for converting numbers to strings and back, so developers had to resort mostly to type-unsafe functions, and they usually wrote their own utility functions in order to avoid writing the same code over and over again. With C++11, the standard library provides utility functions for converting between numbers and strings. In this recipe, you will learn how to convert between numbers and strings and the other way around using modern C++ standard functions.

Getting ready

All the utility functions mentioned in this recipe are available in the <string> header.

How to do it...

Use the following standard conversion functions when you need to convert between numbers and strings:
  • To convert from an integer or floating-point type to a string type, use std::to_string() or std::to_wstring(), as shown in the following code snippet:
    auto si = std::to_string(42); // si="42" auto sl = std::to_string(42L); // sl="42" auto su = std::to_string(42u); // su="42" auto sd = std::to_wstring(42.0); // sd=L"42.000000" auto sld = std::to_wstring(42.0L); // sld=L"42.000000" 
  • To convert from a string type to an integer type, use std::stoi(), std::stol(), std::stoll(), std::stoul(), or std::stoull(), as shown in the following code snippet:
    auto i1 = std::stoi("42"); // i1 = 42 auto i2 = std::stoi("101010", nullptr, 2); // i2 = 42 auto i3 = std::stoi("052", nullptr, 8); // i3 = 42 auto i4 = std::stoi("0x2A", nullptr, 16); // i4 = 42 
  • To convert from a string type to a floating-point type, use std::stof(), std::stod(), or std::stold(), as shown in the following code snippet:
    // d1 = 123.45000000000000 auto d1 = std::stod("123.45"); // d2 = 123.45000000000000 auto d2 = std::stod("1.2345e+2"); // d3 = 123.44999980926514 auto d3 = std::stod("0xF.6E6666p3"); 

How it works...

To convert an integral or floating-point type to a str...

Inhaltsverzeichnis