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

Compartir libro
  1. 750 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
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

Detalles del libro
Vista previa del libro
Índice
Citas

Información del libro

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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Modern C++ Programming Cookbook un PDF/ePUB en línea?
Sí, puedes acceder a Modern C++ Programming Cookbook de Marius Bancila en formato PDF o ePUB, así como a otros libros populares de Computer Science y Programming in C++. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2020
ISBN
9781800206205
Edición
2

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...

Índice