C ++中的计时计时器加倍

问题描述 投票:0回答:1

我想在程序启动以来的时间达到某个倍数(在此示例中为5秒的倍数)时创建一个程序,我希望该程序执行某些操作。当我尝试对duration变量使用%5操作时,我不断收到不同的错误。这是我到目前为止所拥有的:

(at the start of the program i defined start as high_resolution_clock::now())
duration<double> dur = start-high_resolution_clock::now();
if(dur%5==0)

目前,我得到的错误是:没有运算符“ ==”与这些操作数匹配-操作数类型为:std :: chrono :: duration> == int

c++ chrono
1个回答
0
投票

您正在寻找类似的东西:

#include <chrono>

int main() {
    using std::chrono::high_resolution_clock;
    auto start = high_resolution_clock::now();
    bool condition = true;
    while (condition) {
        auto time_passed = start - high_resolution_clock::now();
        if( (time_passed % std::chrono::seconds(5)).count() == 0 ) {
            // do your thing every 5 seconds
        }
        // ...
    }
}

http://coliru.stacked-crooked.com/a/9a13083aa01b339e

© www.soinside.com 2019 - 2024. All rights reserved.