Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ chrono::count() Function



The std::chrono::count() function in C++, provides facilitates for date and time manipulation. It is typically used with chrono::duration objects to retrieve the numerical values representing the tine span in specific units, like seconds or milliseconds.

This function helps to convert the chrono::duration instances into fundamental types (such as int or double) for easy calculations or conversions.

Syntax

Following is the syntax for std::chrono::count() function.

constexpr rep count() const;

Parameters

This function does not accepts any parameters.

Return value

This function returns the number of ticks for this duration.

Example 1

In the following example, we are going to consider the basic usage of the count() function.

#include <iostream>
#include <chrono>
int main() {
   std::chrono::seconds a(11);
   std::cout << "Result : " << a.count() << "s" << std::endl;
   return 0;
}

Output

Output of the above code is as follows −

Result : 11s

Example 2

Consider the following example, we are going to convert the minutes to seconds and using the count() function.

#include <iostream>
#include <chrono>
int main() {
   std::chrono::minutes a(4);
   auto seconds = std::chrono::duration_cast < std::chrono::seconds > (a);
   std::cout << "Result : " << seconds.count() << "s" << std::endl;
   return 0;
}

Output

Following is the output of the above code −

Result : 240s

Example 3

Let's look at the following example, where we are going to convert the milliseconds to seconds and applying the count() function.

#include <iostream>
#include <chrono>
int main() {
   std::chrono::milliseconds a(2000);
   std::cout << "Result : " << std::chrono::duration_cast < std::chrono::seconds > (a).count() << "s" << std::endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

Result : 2s
cpp_chrono.htm
Advertisements