我想在程序启动以来的时间达到某个倍数(在此示例中为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
您正在寻找类似的东西:
#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
}
// ...
}
}