Converting between different time units using std::ratio
Since C++11, the STL contains some new types and functions for taking, measuring, and displaying time. This part of the library exists in the std::chrono
namespace and has some sophisticated details.
In this recipe, we will concentrate on measuring time spans and how to convert the result of the measurement between units, such as seconds, milliseconds, and microseconds. The STL provides facilities, which enable us to define our own time units and convert between them seamlessly.
How to do it...
In this section, we will write a little game that prompts the user to enter a specific word. The time that the user needs to type this word into the keyboard is measured and displayed in multiple time units:
- At first, we need to include all the necessary headers. For reasons of comfort, we declare that we use the
std
namespace by default:
#include <iostream> #include <chrono> #include <ratio> #include <...