英文:
How to extract information of only each 3rd event?
问题
我想只查看每第三个事件,如果满足特定要求的话。例如,在事件循环中,如果事件的值大于15,那么我只想提取每第三个这样的事件的信息。如果事件的值小于15,那么我希望获取每个事件的信息。谢谢!
我是一个初学者,所以我只有一个"理论上"的想法,我想要什么。我搜索了一下,但没有找到类似的问题。
英文:
I would like to look only at every 3rd event if those satisfy some specific requirement. For example, in the event loop, if I have events with a value greater than 15, then I want to extract information only of each 3rd such event. And if the event has value less than 15, then I want to have information of each event. Thank you!
I am a beginner, so I have only a "theoretical" idea of what I want. I searched around and could not find a similar problem.
答案1
得分: 0
这并不是在实际应用中最佳的方法,但如果你制作一个简单的应用程序,你可以使用 if-else 语句来完成这个任务。
std::uint8_t counter = 1;
for (;;) {
const auto event = get_event(); // 这里我们获取事件
if (event.value < 15) { /* 对事件执行你想要的操作 */ }
else {
if (counter % 3 != 0){
++counter;
}
else {
counter = 1;
// 对事件执行你想要的操作
}
}
}
在这里,我们在循环中获取事件,并检查事件的值是否大于15。如果大于15,我们在每第三个事件上执行操作,否则在每个事件上执行操作。
英文:
This is not the best way to do that in real world applications, but if you do some simple app, you can do this using if-else statements
std::uint8_t counter = 1;
for (;;) {
const auto event = get_event(); // there we retrieving our event
if (event.value < 15) { /* do what you want with event */ }
else {
if (counter % 3 != 0){
++counter;
}
else {
counter = 1;
// do what you want with event
}
}
}
Here, we get our event in loop, checking if event value is greater than 15, or not. If it's greater, we do something on every 3rd event, otherwise, do something on every event.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论