Learn C++ by Example: Covers versions 11 to 23 [1 ed.] 1633438333, 9781633438330

Learn the latest features of modern C++ by coding eight engaging projects. Don’t let the multitude of C++ updates intim

125 49 3MB

English Pages 248 Year 2024

Report DMCA / Copyright

DOWNLOAD FILE

Learn C++ by Example: Covers versions 11 to 23 [1 ed.]
 1633438333, 9781633438330

  • Commentary
  • Published: February 2024

Table of contents :
1 Hello again, C++!
1.1 Why does C++ matter?
1.2 When should you use C++?
1.3 Why read this book?
1.4 How does this book teach C++?
1.4.1 Who this book is for
1.4.2 Hello, again, C++!
1.4.3 What you’ll learn from reading this book
1.5 Some pro tips
Summary

2 Containers, iterators, and ranges
2.1 Creating and displaying a vector
2.2 Creating and displaying Pascal’s triangle
2.2.1 A reminder of Pascal’s triangle
2.2.2 Coding Pascal’s triangle
2.2.3 Move semantics and perfect forwarding
2.2.4 Using ranges to display the vector
2.2.5 Using format to display output
2.3 Properties of the triangle
2.3.1 Checking the first and last elements of each row
2.3.2 Checking the number of elements in each row
2.3.3 Checking the sum of the elements in a row
2.3.4 How many rows can we generate correctly?
2.3.5 Checking whether each row is symmetric
2.3.6 Highlighting odd numbers in a row
Summary

3 Input of strings and numbers
3.1 Guessing a predetermined number
3.1.1 Accepting user input the hard way
3.1.2 Accepting optional numeric input
3.1.3 Validation and feedback using std::function and lambdas
3.2 Guessing a random number
3.2.1 Setting up a random number generator
3.2.2 Using the random number generator
3.3 Guessing a prime number
3.3.1 Checking whether the number is prime
3.3.2 Checking properties with static_assert
3.3.3 Generating a random prime number
3.3.4 Deciding which digits are correct
3.3.5 Providing different clues using std::function
Summary

4 Time points, duration, and literals
4.1 How long until the last day of the year?
4.2 Understanding durations in detail
4.2.1 Ratios
4.2.2 Durations
4.2.3 Literal suffixes and operator / for readable code
4.2.4 Requirements and concepts
4.2.5 How many days until the last day of the year?
4.2.6 Using last to find how long to payday
4.2.7 Writing testable code
4.3 Input, output, and formatting
4.3.1 Parsing a date
4.3.2 Formatting time points and durations
4.4 Time zones
Summary

5 Creating and using objects and arrays
5.1 Creating a deck of playing cards
5.1.1 Defining a card type using a scoped enum for the suit
5.1.2 Defining a card type using a strong type for the face value
5.1.3 Constructors and default values
5.1.4 Displaying playing cards
5.1.5 Using an array to make a deck of cards
5.1.6 Using generate to fill the array
5.1.7 Comparison operators and defaults
5.2 Higher-or-lower card game
5.2.1 Shuffling the deck
5.2.2 Building the game
5.2.3 Using std::variant to support cards or jokers
5.2.4 Building the game with an extended deck of cards
Summary

6 Smart pointers and polymorphism
6.1 A class hierarchy
6.1.1 An abstract base class
6.1.2 A concrete class
6.1.3 Warming up for a race
6.1.4 Using type traits to check for special member functions
6.2 Writing and using derived classes in a vector
6.2.1 A blob moving randomly
6.2.2 Smart pointers
6.2.3 Race!
6.2.4 Some design considerations
Summary

7 Associative containers and files
7.1 Hardcoded answer smash
7.1.1 Creating and using an std::map
7.1.2 Pairs, tuples, and structured bindings
7.1.3 A simple answer smash game
7.2 Associative containers
7.2.1 The map type in more detail
7.2.2 Using lower and upper bound to find a key more efficiently
7.2.3 Multimaps
7.3 File-based answer smash
7.3.1 Loading data from a file
7.3.2 Picking a word randomly using std::sample
7.3.3 Answer smash
Summary

8 Unordered maps and coroutines
8.1 Randomly generated matching pennies
8.2 Matching pennies using an unordered_map
8.2.1 Unordered containers and std::hash
8.2.2 Using an unordered_map to make a prediction
8.2.3 The mind reader game
8.3 Coroutines
8.3.1 How to make a coroutine
8.3.2 A coroutine function
8.3.3 The coroutine’s return object
8.3.4 RAII and the rule of zero
8.3.5 Filling in the promise_type
8.3.6 Filling in the Task type
8.3.7 A coroutine mind reader
Summary

9 Parameter packs and std::visit
9.1 The triangle numbers
9.1.1 Testing our triangle numbers with algorithms
9.1.2 Execution policies for algorithms
9.1.3 Mutable lambdas
9.1.4 More properties of the triangle numbers
9.2 A simple slot machine
9.2.1 Revision of constexpr and std::format
9.2.2 Using std::rotate to spin the reels
9.2.3 The simple slot machine
9.3 A better slot machine
9.3.1 Parameter packs and fold expressions
9.3.2 Using a parameter pack to find frequencies
9.3.3 A fairer payout
9.3.4 Allowing holds, nudges, or spins
9.3.5 Spinning reels with std::visit and std::views::zip
Summary
Appendixes

Appendix A: Further resources

Polecaj historie