c++ - 定义分钟单位

标签 c++ boost boost-units

这是来自另一个 question 的一些代码将 29.0 分钟加到 60.0 秒并以小时为单位显示结果:

cout <<
    static_cast<quantity<hour_base_unit::unit_type>>
    (quantity<time>{29.0 * minute_base_unit::unit_type()} + 60.0 * seconds)
    << endl;

定义分钟的推荐方法是什么,以便上面的表达式可以写成:

cout <<
    static_cast<quantity<hour_base_unit::unit_type>>
    (29.0 * minutes + 60.0 * seconds)
    << endl;

最佳答案

如果可以,我建议使用 C++14 的 <chrono>这方面的设施。他们人很好。 (我知道这在技术上不能回答他的问题,但它可能会为他节省大量工作)。

#include <iostream>
#include <chrono>

int main () {
    using namespace std::chrono;
    std::cout << duration_cast<hours>(29min + 60s).count() << std::endl;
}

关于c++ - 定义分钟单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34425499/

相关文章:

c++ - 是否可以在C++中获得CHAR的有效十六进制地址?

c++ - 多个 tcp 套接字,一个停止

c++ - 如何为共享内存映射选择固定地址

c++ - boost/Netbeans : Recursive Includes Related to BSD on Linux Mint 17. 2

c++ - 在 C++ 中使用有界数组的正确方法是什么?

c# - 创建具有 native 依赖项的 NetCore NuGet 包

c++ - 开发-C++/TDM-GCC : Linkage Problems with Boost Libaries Downloaded from boost. 组织

c++ - Physical Boost.Units 用户定义文字

c++ - 有没有理由不使用单位强制类型?