c++ - std::chrono::system_clock 和 duration<double>

标签 c++ c++11 stl c++-chrono

我可以使用以下方法测量时间:

t1 = system_clock::now();
...
t2 = system_clock::now();

duration<double> d = t2 - t1;

编译没问题,但我该怎么做呢?我想用duration移动一个时间点?

示例(不编译):

system_clock::time_point tp1 = system_clock::now();
system_clock::time_point tp2 = tp1 + duration<double>(1.0);

system_clock::time_point '+' 运算符似乎不接受 duration<double> .

如何将时间点移动指定的持续时间(以秒为单位(浮点值))?我需要使用 system_clock因为它可以转换为 time_t .

最佳答案

这是你想要的吗?

system_clock::time_point tp1 = system_clock::now(); 
system_clock::time_point tp2 = tp1 + duration_cast<system_clock::duration>(duration<double>(1.5));
cout << duration_cast<milliseconds>(tp2 - tp1).count();

输出:相差1500ms

Live Demo

关于c++ - std::chrono::system_clock 和 duration<double>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35693959/

相关文章:

c++ - boost::shared_ptr 和 std::shared_ptr 的共存

c++ - 使用 std::vector 的奇怪段错误

c++ - 类成员在其他类中充当友元 C++

c++ - 使用 char* 存储整数

c++ - Visual Studio : How to specify different runtime libraries for the linker?(/MTd、MDd 等)

使用 "invalid"模板参数的 C++ 元编程 - 允许吗?

c++ - VTK 7 - 为什么对象被销毁?

c++ - Depth + Stencil 帧缓冲区问题

c++ - "Predicates should not modify their state due to a function call"是什么意思?

c++ - 如何根据属性从 vector 中提取最大的对象?