Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 开始一个循环_C++_Loops - Fatal编程技术网

C++ 开始一个循环

C++ 开始一个循环,c++,loops,C++,Loops,如果我有一个条件循环,它的行为应该是从一部分开始的,那么什么才是编写此代码的最佳方式?像下面的代码片段一样包含一个goto的代码是否可以接受 goto first_itr; do { doSomeStuff(); first_itr: doSomeOtherStuff(); } while(condition); 我能看到的显然效率较低的替代方法是在doSomeStuff()周围引入一个标志和一个条件,生成以下内容: bool first = true; do { if(first

如果我有一个条件循环,它的行为应该是从一部分开始的,那么什么才是编写此代码的最佳方式?像下面的代码片段一样包含一个
goto
的代码是否可以接受

goto first_itr;
do {
  doSomeStuff();
first_itr:
  doSomeOtherStuff();
} while(condition);
我能看到的显然效率较低的替代方法是在
doSomeStuff()周围引入一个标志和一个条件,生成以下内容:

bool first = true;
do {
  if(first)
    first = false;
  else
    doSomeStuff();
  doSomeOtherStuff();
} while(condition);
这是我在这种情况下通常使用的方法。编译器可以通过有效地将其转换为第一个代码段来改善这一点吗

我能想到的其他替代方法是避免
goto
,条件可能涉及重复代码,但这似乎比
goto
更糟糕

另外,如果循环是性能关键型的,并且额外条件的开销显著降低了性能,那么答案会有所不同吗?

可能是这样的:

for (;;)
{
    doSomeOtherStuff();
    if (!condition) break;
    doSomeStuff();
}

如果A=
doSomeStuff(),B=
doSomeOtherStuff(),c表示条件测试,然后我们要编码

BcABcABcABcABc ...
这可以通过在问题中的
ABc
块上循环(并在第一次迭代中以某种方式避免
A
)或在本解决方案中的块
BcA
来实现。与其他解决方案相比,此解决方案的优点在于避免了臭名昭著的代码重复。

可能如下:

for (;;)
{
    doSomeOtherStuff();
    if (!condition) break;
    doSomeStuff();
}

如果A=
doSomeStuff(),B=
doSomeOtherStuff(),c表示条件测试,然后我们要编码

BcABcABcABcABc ...
这可以通过在问题中的
ABc
块上循环(并在第一次迭代中以某种方式避免
A
)或在本解决方案中的块
BcA
来实现。与所有其他解决方案相比,此解决方案的优势在于它避免了代码重复,而这是臭名昭著的维护问题。

使用您的更新:

如果希望在第一次迭代中跳过函数调用,只需将其从循环结构中完全删除即可。无需在循环中粘贴不需要的条件:

doSomeOtherStuff(); // your first iteration is outside the loop
do
{
    doSomeStuff(); // all other iterations inside the loop
    doSomeOtherStuff();
} while (condition);
如果
doSomeOtherStuff
条件有影响,则应将其改写为:

doSomeOtherStuff(); // your first iteration is outside the loop
while (condition)
{
    doSomeStuff(); // all other iterations inside the loop
    doSomeOtherStuff();
}
这是假设操作顺序很重要(例如,如果函数对数组中的元素进行操作,而您只希望第一个元素被其中一个函数修改)。如果不重要(例如,您只想分别运行函数
X
X-1
次),您可以使用Antti或Kerrek的解决方案。

和您的更新:

如果希望在第一次迭代中跳过函数调用,只需将其从循环结构中完全删除即可。无需在循环中粘贴不需要的条件:

doSomeOtherStuff(); // your first iteration is outside the loop
do
{
    doSomeStuff(); // all other iterations inside the loop
    doSomeOtherStuff();
} while (condition);
如果
doSomeOtherStuff
条件有影响,则应将其改写为:

doSomeOtherStuff(); // your first iteration is outside the loop
while (condition)
{
    doSomeStuff(); // all other iterations inside the loop
    doSomeOtherStuff();
}

这是假设操作顺序很重要(例如,如果函数对数组中的元素进行操作,而您只希望第一个元素被其中一个函数修改)。如果它不重要(例如,您只想分别运行函数
X
X-1
次),您可以使用Antti或Kerrek的解决方案。

我可能会在循环之前重复
doSomeOtherStuff()
,但@Kerrek SB的解决方案也不错(特别是如果你不想在函数中使用它的话)。这是风格的问题

doSomeOtherStuff();
while(condition){
  doSomeStuff();
  doSomeOtherStuff();
}

我可能会在循环之前重复
doSomeOtherStuff()
(你必须在函数中使用它),但是@Kerrek SB的解决方案也很好(特别是如果你不想在函数中使用它的话)。这是一个风格问题

doSomeOtherStuff();
while(condition){
  doSomeStuff();
  doSomeOtherStuff();
}

>代码> Goto < /C> >虽然很难看。但是,您不能与初始化代码和“代码”> Goto < /> >交叉。除此之外,在我的理解中, Goto < /C> >在C++中是允许的。

作为一个
for
循环和重复代码的建议,我开玩笑地建议在这里滥用逗号运算符(前提是
运算符,
对于
doSomeOtherStuff()
的返回值没有重载):


如果
A=doSomeStuff()
B=doSomeOtherStuff()
C=condition
,这将导致序列
BCABCABC…
与原始代码相同;它还将在
C
第一次计算为false时结束。如果您可以编写
doSomeOtherStuff())Kerrek SB的for循环几乎可以肯定是一个更好的答案。

< p>虽然<丑> Goto < /Cord>是允许的,虽然很难看。但是,你不能用你的<代码> Goto < /C> >交叉初始化器。除此之外,我认为C++中的<代码> Goto < /C> >块是允许的。 作为一个
for
循环和重复代码的建议,我开玩笑地建议在这里滥用逗号运算符(前提是
运算符,
对于
doSomeOtherStuff()
的返回值没有重载):


如果
A=doSomeStuff()
B=doSomeOtherStuff()
C=condition
,这将导致序列
BCABCABC…
与原始代码相同;它还将在
C
第一次计算为false时结束。如果您可以编写
doSomeOtherStuff())
作为一个表达式。Kerrek SB的for-loop几乎肯定是一个更好的答案。

虽然我接受了Kerrek SB的答案,但出于完整性考虑(以及对的扭曲改编),我只想添加另一个答案。它不是(技术上)使用
goto
,它不会复制代码,尽管良好实践的可读性和使用性值得怀疑

switch(true) {
case false:
  do {
    doSomeStuff();
case true:
    doSomeOtherStuff();
  } while(condition);
}

虽然我已经接受了Kerrek SB的答案,但为了完整起见(同时也是由于对的扭曲改编),我只想添加另一个答案