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

Wojtek

我正在学习<chrono>图书馆,正在考虑std::chrono::duration上课,是否有任何特定的理由以秒为基础?例如,存储秒的变量将是

chrono::duration<int> two_seconds(2);

而所有其他时间跨度都需要将它们与秒相关联,例如

chrono::duration<int, ratio<60>> two_minutes(2);
chrono::duration<int, ratio<1, 1000>> two_milliseconds(2);
chrono::duration<int, ratio<60 * 60 * 24>> two_days(2); 

是否有任何理由将持续时间基于秒而不是分钟,小时等?

莫尔布德尼洛

之所以选择秒,是因为它是SI系统和整个科学中的基本时间单位。
即使是美国人,也使用秒,而不是像microfortnights。

为什么这个基本时间跨度不是持续时间类的另一个模板参数

是的,因为您可以为typedefs提供比率,其中一些包含在标准中。

#include <chrono>

std::chrono::duration<int, minutes> two_minutes(2);            // Standard
std::chrono::duration<int, milliseconds> two_milliseconds(2);  // Standard

如果您需要更多,可以轻松添加:

typedef std::ratio<60 * 60 * 24> days;
typedef std::ratio<756, 625> microfortnights;

std::chrono::duration<int, days> two_days(2); 
std::chrono::duration<int, microfortnights> two_weeks(1000000); 

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么 std::chrono::duration::operator*= 不像内置的 *=?

来自分类Dev

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

来自分类Dev

std :: chrono :: duration_cast-比纳秒还精确的单位?

来自分类Dev

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

来自分类Dev

std :: chrono :: duration :: zero()的用途是什么

来自分类Dev

std :: chrono :: duration :: zero()的用途是什么

来自分类Dev

子类化std :: chrono :: duration

来自分类Dev

std :: chrono :: duration_cast的奇怪结果持续1秒和2秒

来自分类Dev

为什么std :: chrono说我的函数需要零纳秒的时间执行?

来自分类Dev

std :: chrono :: duration对象的绝对值

来自分类Dev

C ++获取std :: chrono :: duration的周期

来自分类Dev

C ++ 20 std :: chrono :: duration格式的缺点

来自分类Dev

std :: chrono :: duration作为方法参数传递

来自分类Dev

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

来自分类Dev

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

来自分类Dev

是否在std :: chrono :: duration中定义了std :: ratio计算顺序?

来自分类Dev

C ++:std :: chrono或boost :: chrono

来自分类Dev

接受任何表示/时间段的std :: chrono :: duration

来自分类Dev

如何使用std :: chrono :: duration作为模板参数?

来自分类Dev

std :: chrono :: system_clock和duration <double>

来自分类Dev

通过值或引用const传递std :: chrono :: duration?

来自分类Dev

将std :: chrono :: duration存储为成员的类?

来自分类Dev

std :: chrono :: duration_cast count()返回零

来自分类Dev

是std :: chrono :: duration默认初始化为0

来自分类Dev

无法将 std::chrono::duration 与零进行比较

来自分类Dev

以下typedef在chrono :: duration中是什么意思?

来自分类Dev

从“ LLONG_MAX秒”构造一个std :: chrono :: milliseconds变量时会发生什么?

来自分类Dev

C ++ chrono :: duration_cast始终输出“ 0秒”

来自分类Dev

如何编写一个采用任何std :: chrono :: duration(1ms,4s,7h)并以秒为单位的秒数的函数?

Related 相关文章

  1. 1

    为什么 std::chrono::duration::operator*= 不像内置的 *=?

  2. 2

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

  3. 3

    std :: chrono :: duration_cast-比纳秒还精确的单位?

  4. 4

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

  5. 5

    std :: chrono :: duration :: zero()的用途是什么

  6. 6

    std :: chrono :: duration :: zero()的用途是什么

  7. 7

    子类化std :: chrono :: duration

  8. 8

    std :: chrono :: duration_cast的奇怪结果持续1秒和2秒

  9. 9

    为什么std :: chrono说我的函数需要零纳秒的时间执行?

  10. 10

    std :: chrono :: duration对象的绝对值

  11. 11

    C ++获取std :: chrono :: duration的周期

  12. 12

    C ++ 20 std :: chrono :: duration格式的缺点

  13. 13

    std :: chrono :: duration作为方法参数传递

  14. 14

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

  15. 15

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

  16. 16

    是否在std :: chrono :: duration中定义了std :: ratio计算顺序?

  17. 17

    C ++:std :: chrono或boost :: chrono

  18. 18

    接受任何表示/时间段的std :: chrono :: duration

  19. 19

    如何使用std :: chrono :: duration作为模板参数?

  20. 20

    std :: chrono :: system_clock和duration <double>

  21. 21

    通过值或引用const传递std :: chrono :: duration?

  22. 22

    将std :: chrono :: duration存储为成员的类?

  23. 23

    std :: chrono :: duration_cast count()返回零

  24. 24

    是std :: chrono :: duration默认初始化为0

  25. 25

    无法将 std::chrono::duration 与零进行比较

  26. 26

    以下typedef在chrono :: duration中是什么意思?

  27. 27

    从“ LLONG_MAX秒”构造一个std :: chrono :: milliseconds变量时会发生什么?

  28. 28

    C ++ chrono :: duration_cast始终输出“ 0秒”

  29. 29

    如何编写一个采用任何std :: chrono :: duration(1ms,4s,7h)并以秒为单位的秒数的函数?

热门标签

归档