如何将std :: chrono :: duration转换为double(秒)?

PřemyslŠťastný

来吧using duration = std::chrono::steady_clock::duration我想用最大的精度优雅地将持续时间转换为以秒为单位的两倍。

我发现了相反的方法(将秒转换为std :: chrono :: duration?的两倍),但这并没有帮助我找到答案。

换句话说,我想要最佳的std函数double F(duration),该函数返回秒。

谢谢。

橡子

只需做:

std::chrono::duration<double>(d).count()

或者,作为一个功能:

template <class Rep, class Period>
constexpr auto F(const std::chrono::duration<Rep,Period>& d)
{
    return std::chrono::duration<double>(d).count();
}

如果您需要std::chrono::duration构造函数无法实现的更复杂的类型转换,请使用std::chrono::duration_cast

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将秒转换为std :: chrono :: duration吗?

如何将 std::chrono::duration 转换为提升持续时间

如何将'std :: chrono :: duration <int,std :: ratio <2629746l,1l>>'转换为'int'类型?

将std :: string转换为std :: chrono :: duration

将chrono :: duration类型转换为std :: tm类型

如何将chrono :: steady_clock :: duration转换为ostream?

如何将std :: chrono :: high_resolution_clock :: now()转换为毫秒,微秒,...?

C ++如何将std :: chrono :: time_point转换为long并返回

如何将std :: chrono :: time_point转换为字符串

如何将`std :: chrono :: milliseconds`转换为`boost :: posix_time :: milliseconds`

如何将“std::chrono::system_clock::now()”转换为双倍

如何将 std::chrono::high_resolution 类型转换为浮点类型?

为什么std :: chrono :: duration基于秒

如何将chrono :: format :: strftime静态转换为chrono :: format :: Item`?

如何将YYYY / MM / DD HH:MM:SS转换为std :: chrono :: system_clock :: time_point?

如何在C ++中声明std :: chrono :: duration <double>变量

子类化std :: chrono :: duration

C ++,如何缩放std :: chrono :: duration?

std :: chrono :: duration默认如何构造?

如何概括 std::chrono::duration(s)?

飞跃秒和std :: chrono

从struct timespec转换为std :: chrono ::?

使用std :: chrono / date :: gps_clock将double gps时间戳转换为utc / tai

将chrono :: duration转换为字符串或C字符串

std::chrono::duration 与不同代表之间转换的意外错误

C++ chrono:如何将整数转换为时间点

std :: hash表示std :: chrono :: duration

std :: chrono :: duration :: duration()如何才能成为constexpr?

使用std :: chrono :: duration跟踪超时