I have a stupid problem. I try to switch to the c++11 headers and one of those is chrono. But my problem is that I cant cout the result of time operations. For example:
auto t=std::chrono::high_resolution_clock::now(); cout<<t.time_since_epoch();
gives:
initializing argument 1 of
‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::chrono::duration<long int, std::ratio<1l, 1000000l> >]’
... /usr/include/c++/4.6/ostream
cout<<(uint64_t)t.time_since_epoch();
gives invalid cast
C++ includes support for two types of time manipulation: The chrono library, a flexible collection of types that track time with varying degrees of precision (e.g. std::chrono::time_point). C-style date and time library (e.g. std::time)
std::cout is used to output a value (cout = character output) std::cin is used to get an input value (cin = character input) << is used with std::cout, and shows the direction that data is moving (if std::cout represents the console, the output data is moving from the variable to the console).
Difference between cout and std::cout in C++ The cout is a predefined object of ostream class, and it is used to print the data on the standard output device. Generally, when we write a program in Linux operating system for G++ compiler, it needs “std” namespace in the program.
There is no std::cout in C. In a windowing system, the std::cout may not be implemented because there are windows and the OS doesn't know which one of your windows to output to. never ever give cout NULL. it will stop to work.
As others have noted, you can call the count()
member function to get the internal count.
I wanted to add that I am attempting to add a new header: <chrono_io>
to this library. It is documented here. The main advantage of <chrono_io>
over just using count()
is that the compile-time units are printed out for you. This information is of course obtainable manually, but it is much easier to just have the library to it for you.
For me, your example:
#include <iostream> #include <chrono_io> int main() { auto t = std::chrono::high_resolution_clock::now(); std::cout << t.time_since_epoch() << '\n'; }
Outputs:
147901305796958 nanoseconds
The source code to do this is open source and available at the link above. It consists of two headers: <ratio_io>
and <chrono_io>
, and 1 source: chrono_io.cpp
.
This code should be considered experimental. It is not standard, and almost certainly will not be standardized as is. Indeed preliminary comments from the LWG indicate that they would prefer the default output to be what this software calls the "short form". This alternative output can be obtained with:
std::cout << std::chrono::duration_fmt(std::chrono::symbol) << t.time_since_epoch() << '\n';
And outputs:
147901305796958 ns
It only took a decade, but C++20 now does:
#include <chrono> #include <iostream> int main() { auto t = std::chrono::high_resolution_clock::now(); std::cout << t.time_since_epoch() << '\n'; }
Output:
147901305796958ns
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With