如何在 C++ 中初始化时间点并将它们设置为假值?
How initialize time points and set them to a falsy value afterward in C++?
我是 C++ 的新手,我想实现以下行为。
#include <thread>
#include <chrono>
#include <iostream>
using namespace std;
int main(){
chrono::high_resolution_clock::time_point start_time_point;
while(true){
if(statement_1){
// check whether start_time_point is falsy
if(...) start_time_point = chrono::high_resolution_clock::now();
auto current_time_point = chrono::high_resolution_clock::now();
if(chrono::duration_cast<std::chrono::seconds>(current_time_point - start_time_point).count() > 5) {
// do something
}
}
if(statement_2){
// assign some falsy value to start_time_point
// so that next time the first if block is entered a new time point is assigned to start_time_point
}
}
}
我习惯了javascript,所以在JS中我只会在第二个if块中将null
赋值给start_time_point
,但在C++中是不同的。我将如何实现上述行为?
I am used to javascript, so in JS I only would assign null to
start_time_point in the second if block but in C++ it is different.
How would I achieve the above-mentioned behavior?
一个chrono::time_point
有一个min()
成员,返回可能的最低时间点。它不完全相同,但可以 与 javascript.
中的空值相似
也许
start_time_point = start_time_point.min();
我是 C++ 的新手,我想实现以下行为。
#include <thread>
#include <chrono>
#include <iostream>
using namespace std;
int main(){
chrono::high_resolution_clock::time_point start_time_point;
while(true){
if(statement_1){
// check whether start_time_point is falsy
if(...) start_time_point = chrono::high_resolution_clock::now();
auto current_time_point = chrono::high_resolution_clock::now();
if(chrono::duration_cast<std::chrono::seconds>(current_time_point - start_time_point).count() > 5) {
// do something
}
}
if(statement_2){
// assign some falsy value to start_time_point
// so that next time the first if block is entered a new time point is assigned to start_time_point
}
}
}
我习惯了javascript,所以在JS中我只会在第二个if块中将null
赋值给start_time_point
,但在C++中是不同的。我将如何实现上述行为?
I am used to javascript, so in JS I only would assign null to start_time_point in the second if block but in C++ it is different. How would I achieve the above-mentioned behavior?
一个chrono::time_point
有一个min()
成员,返回可能的最低时间点。它不完全相同,但可以 与 javascript.
也许
start_time_point = start_time_point.min();