gpt4 book ai didi

c++ - 如何将int转换为chrono毫秒

转载 作者:行者123 更新时间:2023-12-05 00:46:27 25 4
gpt4 key购买 nike

我有一个毫秒类型的变量,我想将它设置为等于一个等式。这是我到目前为止所做的。它编译并运行,但是当它到达这一行时,它会停止输入下面的 if 语句。

time = duration<int>(750-(lvl*50));

要注意的另一件事是,我确实有一个 if 语句,这也可能是我比较不同数据类型的问题的一部分。这是 if 语句:

if(time_since_last >= time) {

距离上一个变量的时间是不同的2个high_resolution_clock::now()的时间差

最佳答案

你可以试试这样的。要以毫秒为单位将整数值转换为 chrono,请使用 std::chrono::milliseconds(value);

auto old_time = std::chrono::high_resolution_clock::now();
this_thread::sleep_for(chrono::milliseconds(500));
auto new_time = std::chrono::high_resolution_clock::now();
auto time_since_last = std::chrono::duration_cast<chrono::milliseconds>(new_time - old_time);
cout << time_since_last.count();

int value = 1000;
auto time = std::chrono::milliseconds(value);
cout << " " <<time.count();

if (time_since_last >= time) {
/* do something */
}

关于c++ - 如何将int转换为chrono毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60480907/

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