如何及时执行if语句?
How to execute if statement in time?
我以这种方式减慢了 if 语句的执行速度。但我不喜欢他。
for (size_t i = 0;;) {
i = (i + 1) % 1000000;
if (i == 10) {
cout << " TEST " << "n" << endl;
}
}
我想按时制作代码。 在一分钟过去之前,应始终传递 if 语句。
如何实现这一点?
for (;;) {
/* do something */
if (one_minute_elapsed_since_first_iteration) {
/* do something else */
}
}
还有一个问题,它会比第一个选项工作得更快吗?
如果你只是想在你的if
中添加延迟,那么 std::this_thread::sleep_for 可能是你想要的。它会让您的代码进入睡眠状态一段时间,然后在时间到期后继续运行。
例如,要睡一分钟,你可以这样做
#include <chrono>
#include <thread>
...
using namespace std::chrono_literals;
...
std::this_thread::sleep_for(1m);
要回答更新的问题:
"这怎么可能实现?
for (;;) {
/* do something */
if (one_minute_elapsed_since_first_iteration) {
/* do something else */
}
}
">
您需要在循环开始时获取时间戳,然后检查是否超过所需时间。例如:
#include <chrono>
using namespace std::chrono_literals;
auto start = std::chrono::steady_clock::now();
for (;;) {
/* Do something */
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::seconds>(now - start) >= 60s) {
/* Do something else */
/* If we should again wait 1 min. Reset start time */
start = std::chrono::steady_clock::now();
}
}
相关文章:
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复