gpt4 book ai didi

c++ - 将 minutes::rep 转换为 hours::rep

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:51:14 27 4
gpt4 key购买 nike

我应该如何继续将 std::chrono::minutes::rep 类型值转换为小时表示。

#include <iostream>
#include <chrono>

using namespace std;
using namespace std::chrono;

int main() {
minutes::rep time = 4;
std::cout << time; // outputs as minutes, need in hours

// duration cast doesn't seems to work here because it
// needs minutes instead of minutes::rep probably

return 0;
}

最佳答案

minutes::rep time = 4;
std::cout << time; // outputs as minutes, need in hours

因为 time 只是一个(实现定义的)整数类型,它与 minuteshours 无关,并且对基础时间表示一无所知。

您想停留在duration 领域:

minutes time{4};
auto as_hours = std::duration_cast<hours>(time);
std::cout << as_hours.count(); // prints 0

或者,可能:

auto as_hours_dbl = std::duration_cast<duration<double, hours::period>>(time);
std::cout << as_hours_dbl.count(); // prints 0.0666667

关于c++ - 将 minutes::rep 转换为 hours::rep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39898935/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com