“for 循环”的终止条件是否是“for 循环”? 在VC中刷新++ 6?
for (int i = 0 ; i < stlVector.size() ; i++)
{
if (i == 10)
{
stlVector.erase(stlVector.begin() + 5 )
}
}
终止条件部分“stlVector.size()”是否采用“stlVector.erase(...)” 考虑在内? 换句话说,stlVector.size() 是否会为每次循环迭代刷新? 我现在无法测试它,所以我在这里发布了一个问题。
提前谢谢!
最好的问候,
正通
for (int i = 0 ; i < stlVector.size() ; i++)
{
if (i == 10)
{
stlVector.erase(stlVector.begin() + 5 )
}
}
Does the termination condition part "stlVector.size()" take "stlVector.erase(...)"
into consideration? In other word does stlVector.size() refresh for every loop iteration?
I can't test it right now, so i posted a question here.
Thx in advance!
Best regards,
zhengtonic
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
是的,针对每个循环执行测试,但有副作用。
for 循环只是一个很好的约定 - for 循环很容易分解为 while 循环:
变成:
-Adam
Yes, the test is performed, with side effects, for each loop.
A for loop is merely a nice convention - a for loop is easily decomposed as a while loop:
Becomes:
-Adam
是的,确实如此,但不要这样做! 如果要从向量中删除元素,请在另一个循环中执行此操作。 在这种情况下,您将删除 i 索引之后的元素:无法保证 stlVector[i+5] 元素存在。 如果从向量中删除第 i 个元素,则计数会被破坏,因为您可以在不检查元素的情况下跳转元素。
最安全的方法是存储要在另一个向量上删除的 stlVector 上元素的引用,然后迭代此辅助向量执行 stlVector.erase(auxVector[i])。
Yes, it does, but don't do this! If you want to remove elements from a vector, do it inside another loop. You are deleting elements after the i index in this case: nothing guarantees that the stlVector[i+5] element exists. If you remove the i-th element from the vector, your count is broken because you can jump elements without checking them.
The most safe way of doing this is storing references for the elements on the stlVector you want to delete on another vector, and then iterate on this auxiliar vector doing stlVector.erase(auxVector[i]).
我希望您提供的代码只是“幻想代码”(正如一位评论者所说),以给出您想要做的事情类型的具体示例。
但是,以防万一:您给出的循环将跳过第 12 个元素(即最初位于
stlVector[11]
中的元素),因为在检查stlVector[ 10]
你删除了一个较早的元素,导致所有后面的元素向前分流一个位置,但你仍然在循环结束时递增i
。 因此,下一次迭代将查看stlVector[11]
,它实际上是最初位于stlVector[12]
中的元素。 要解决此问题,您需要在调用erase()
之后添加--i
。I expect the code you provided is just "fantasy code" (as one commenter put it) to give a concrete example of the type of thing you're trying to do.
However just in case it's not: the loop you gave will skip over the 12th element (i.e. the element originally in
stlVector[11]
) because when examiningstlVector[10]
you delete an earlier element, causing all later elements to shunt forward one position, but you still incrementi
at the end of the loop. So the next iteration will look atstlVector[11]
which is actually the element that was originally instlVector[12]
. To remedy this, you need to--i
after the call toerase()
.一定要重新评估!
Always reevaluate sure!
另外,为了澄清一点,因为你问“在 VC++ 6 中”是否是这样完成的。
“继续条件”在每个版本的 C、C++、C# 和 Java 中的每个循环上重新计算。
如果任何编译器不生成执行此操作的代码,则该代码已损坏,并且必须避免。
Also, to clarify a bit, since you asked if it's done this way "in VC++ 6".
The "continue condition" is re-evaluate on every loop in EVERY version of C, C++, C# and Java.
If any complier does not generate code which does that, it is broken, and must be avoided.
正如其他人所说,是的,每次循环都会重新评估条件。 这就是为什么常见的性能优化是:
其中循环条件的计算成本很高,而不是
每次循环迭代都不必要地进行昂贵的计算。
As others have said, yes the condition is re-evaluated each time through the loop. That's why a common performance optimization is:
where the loop conditional is at all expensive to compute, instead of
Where the expensive computation is needlessly done each iteration through the loop.
是的,它减小了尺寸。 更多信息请参见此处
Yes it reduces the size. More information is here
需要明确的是,不要将其视为刷新任何内容的循环。 每次检查条件时(在每次循环开始时),都会对 stlVector 变量调用 size() 方法,并返回向量的当前大小。
擦除()方法减少了向量的大小,因此下次调用size()时,返回的值将更小。
Just to be clear, don't think of it in terms of the loop refreshing anything. Every time the condition is checked (at the start of each time through the loop), the size() method is called on the stlVector variable, and the current size of the vector is returned.
The erase() method reduces the size of the vector, so the next time size() is called, the returned value will be smaller.
是的,它确实!
因此,对于每个循环,您都将重新评估测试“i < stlVector.size ()”!
Yes it does!
Thus for every loop you'll have the test "i < stlVector.size ()" re-evaluate!