我从 How to get duration, as int milli’s and float seconds from
#include <chrono>
#include <iostream>
int main (int argc, char *argv[])
{
auto t0 = std::chrono::high_resolution_clock::now();
auto t1 = std::chrono::high_resolution_clock::now();
std::chrono::duration< double > fs = t1 - t0;
std::chrono::milliseconds d = std::chrono::duration_cast< std::chrono::milliseconds >( fs );
std::cout << fs.count() << "s\n";
std::cout << d.count() << "ms\n";
}
哪个完美,但是我如何创建时间戳:
hour:minute:second:millisecond:microsecond:nanosecond
使用 auto t0 = std::chrono::high_resolution_clock::now()
值?
我试图打印 auto t0 = std::chrono::high_resolution_clock::now();
值,看看里面有什么,但它只给了我一个很大的错误堆栈:
#include <chrono>
#include <iostream>
int main (int argc, char *argv[])
{
auto t0 = std::chrono::high_resolution_clock::now();
std::cout << t0 << "\n";
}
错误:
main2.cpp: In function 'int main(int, char**)':
main2.cpp:10:13: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long long int, std::ratio<1, 1000000000> > >')
std::cout << t0 << "\n";
~~~~~~~~~~^~~~~
原文由 user 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以像这样打印
chrono::timepoint
: