如何提取仅每第3个事件的信息?

huangapple go评论60阅读模式
英文:

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 &lt; 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.

huangapple
  • 本文由 发表于 2023年2月27日 19:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定