C++ 如何在没有外部标志的情况下在循环内仅运行一次代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17701197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run code inside a loop only once without external flag?
提问by danijar
I want to check a condition inside a loop and execute a block of code when it's first met. After that, the loop might repeat but the block should be ignored. Is there a pattern for that? Of course it's easy to declare a flag outside of the loop. But I I'm interested in an approach that completely lives inside the loop.
我想检查循环内的条件并在第一次遇到时执行代码块。之后,循环可能会重复,但块应该被忽略。有这样的模式吗?当然,在循环外声明一个标志很容易。但我对一种完全存在于循环中的方法感兴趣。
This example is not what I want. Is there a way to get rid of the definition outside of the loop?
这个例子不是我想要的。有没有办法摆脱循环外的定义?
bool flag = true;
for (;;) {
if (someCondition() && flag) {
// code that runs only once
flag = false;
}
// code that runs every time
}
采纳答案by Angew is no longer proud of SO
It's fairly hacky, but as you said it's the application main loop, I assume it's in a called-once function, so the following should work:
它相当笨拙,但正如您所说,它是应用程序主循环,我假设它位于一次调用函数中,因此以下应该起作用:
struct RunOnce {
template <typename T>
RunOnce(T &&f) { f(); }
};
:::
while(true)
{
:::
static RunOnce a([]() { your_code });
:::
static RunOnce b([]() { more_once_only_code });
:::
}
回答by Alec Jacobson
For a less convoluted version of Mobius's answer:
对于 Mobius 答案的不太复杂的版本:
while(true)
{
// some code that executes every time
for(static bool first = true;first;first=false)
{
// some code that executes only once
}
// some more code that executes every time.
}
You could also write this using ++
on a bool, but that's apparently deprecated.
您也可以使用++
bool编写此代码,但这显然已被弃用。
回答by Mobius
a possibly cleaner way to write this, albeit still with a variable, would be as follows
一种可能更简洁的写法,尽管仍然使用变量,如下所示
while(true){
static uint64_t c;
// some code that executes every time
if(c++ == 0){
// some code that executes only once
}
// some more code that executes every time.
}
The static
allows you to declare the variable inside the loop, which IMHO looks cleaner. If your code that executes every time makes some testable change, you could get rid of the variable and write it like this:
将static
允许你声明的循环,这恕我直言看起来滤清器内的变量。如果每次执行的代码都进行了一些可测试的更改,则可以删除该变量并像这样编写:
while(true){
// some code that executes every time
if(STATE_YOUR_LOOP_CHANGES == INITIAL_STATE){
// some code that executes only once
}
// some more code that executes every time.
}
回答by Guanxi
If you know you only want to run this loop once, why not use break
as the last statement in the loop.
如果您知道只想运行此循环一次,为什么不将其break
用作循环中的最后一条语句。
回答by Meg
1 while(true)
2 {
3 if(someCondition())
4 {
5 // code that runs only once
6 // ...
7 // Should change the value so that this condition must return false from next execution.
8 }
9
10 // code that runs every time
11 // ...
12 }
If you expecting the code without any external flag then you need to change the value of condition in last statement of the condition. (7th line in code snippet)
如果您希望代码没有任何外部标志,那么您需要在条件的最后一条语句中更改条件的值。(代码片段中的第 7 行)