14 The Power Ranges – C++ Ranges; Date and time handling

 

This chapter covers

  • Understand what ranges are and how they simplify and improve your code
  • Exploring the concepts of pipelines, lazy evaluation, views, actions, and compositions
  • Learning how to handle time using the chrono library
  • Writing two computer games – A secret knock game and a slot machine game

In this chapter, we dive into some very powerful features of the most advanced form of C++. We kick things off with Ranges – a powerful new way to handle sequences of elements in different containers. You'll get to grips with exciting new terms like pipelines, views, lazy evaluation, actions, and more. You will understand why iterators and algorithms you learned about in Chapter 11, can get clunky, and how ranges revolutionize the way we iterate through collections, making our code simpler, more powerful, and just plain awesome.

We will also practice a bunch of examples using various range methods, including some fresh ones from the upcoming C++23 standard.

14.1 A whole new range of possibilities – The C++ ranges library

14.1.1 Ranges – a few key concepts before we begin()

14.1.2 Staying within range: Implementing ranges in our code

14.1.3 Final Exercise – an amazing maze

14.2 It’s about time (literally): Introduction to the chrono library

14.2.1 Time is of the essence: basic time concepts and building blocks

14.2.2 The Chrono way – A true delight, all day and all night

14.2.3 Using arithmetic and comparison on time points and durations

14.2.4 Rain in Spain and hails in Wales: Handling different time zones

14.2.5 Timeout! - Measuring time intervals and enforce timeouts

14.3 Knock, knock, who’s there? Code exercises

14.3.1 A secrete knock on the door

14.3.2 Code practice: Play the slot machine like you’re in Vegas

14.4 Summary