“for 循环”的终止条件是否是“for 循环”? 在VC中刷新++ 6?

发布于 2024-07-13 12:08:23 字数 332 浏览 18 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

九厘米的零° 2024-07-20 12:08:24

是的,针对每个循环执行测试,但有副作用。

for 循环只是一个很好的约定 - for 循环很容易分解为 while 循环:

for (int i = 0 ; i < stlVector.size() ; i++)
{ 
    if (i == 10)
    { 
        stlVector.erase(stlVector.begin() + 5 )
    }
}

变成:

int i = 0 ;

while(i < stlVector.size())
{ 
    if (i == 10)
    { 
        stlVector.erase(stlVector.begin() + 5 )
    }
    i++;
}

-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:

for (int i = 0 ; i < stlVector.size() ; i++)
{ 
    if (i == 10)
    { 
        stlVector.erase(stlVector.begin() + 5 )
    }
}

Becomes:

int i = 0 ;

while(i < stlVector.size())
{ 
    if (i == 10)
    { 
        stlVector.erase(stlVector.begin() + 5 )
    }
    i++;
}

-Adam

梦太阳 2024-07-20 12:08:24

是的,确实如此,但不要这样做! 如果要从向量中删除元素,请在另一个循环中执行此操作。 在这种情况下,您将删除 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]).

青丝拂面 2024-07-20 12:08:24

我希望您提供的代码只是“幻想代码”(正如一位评论者所说),以给出您想要做的事情类型的具体示例。

但是,以防万一:您给出的循环将跳过第 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 examining stlVector[10] you delete an earlier element, causing all later elements to shunt forward one position, but you still increment i at the end of the loop. So the next iteration will look at stlVector[11] which is actually the element that was originally in stlVector[12]. To remedy this, you need to --i after the call to erase().

不即不离 2024-07-20 12:08:24

一定要重新评估!

Always reevaluate sure!

绮烟 2024-07-20 12:08:24

另外,为了澄清一点,因为你问“在 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.

多彩岁月 2024-07-20 12:08:24

正如其他人所说,是的,每次循环都会重新评估条件。 这就是为什么常见的性能优化是:

int saveSize = someExpensiveComputation();

for (int i = 0 ; i < saveSize ; i++)
{ 
    foo(i);
}

其中循环条件的计算成本很高,而不是

for (int i = 0 ; i < someExpensiveComputation(); i++)
{ 
    foo(i);
}

每次循环迭代都不必要地进行昂贵的计算。

As others have said, yes the condition is re-evaluated each time through the loop. That's why a common performance optimization is:

int saveSize = someExpensiveComputation();

for (int i = 0 ; i < saveSize ; i++)
{ 
    foo(i);
}

where the loop conditional is at all expensive to compute, instead of

for (int i = 0 ; i < someExpensiveComputation(); i++)
{ 
    foo(i);
}

Where the expensive computation is needlessly done each iteration through the loop.

木槿暧夏七纪年 2024-07-20 12:08:24

是的,它减小了尺寸。 更多信息请参见此处

Yes it reduces the size. More information is here

冷心人i 2024-07-20 12:08:23

需要明确的是,不要将其视为刷新任何内容的循环。 每次检查条件时(在每次循环开始时),都会对 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.

时光磨忆 2024-07-20 12:08:23

是的,它确实!

stlVector.size () // is called for evey iteration

因此,对于每个循环,您都将重新评估测试“i < stlVector.size ()”!

Yes it does!

stlVector.size () // is called for evey iteration

Thus for every loop you'll have the test "i < stlVector.size ()" re-evaluate!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文