我需要从末尾到开头迭代一个向量。 “正确”的方法是
for(std::vector<SomeT>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit)
{
//do Something
}
当 //do Something
涉及到知道实际索引时,则需要使用 rit
进行一些计算才能获得它,例如 index = v.size() - 1 - (rit - v.rbegin)
如果无论如何都需要索引,那么我坚信最好使用该索引进行迭代
for(int i = v.size() - 1; i >= 0; --i)
{
//do something with v[i] and i;
}
这会发出警告:i
是有符号的,而 v.size()
是无符号的。
更改为
for(unsigned i = v.size() - 1; i >= 0; --i)
在功能上是错误的,因为这本质上是一个无限循环 :)< /code>
什么是一种美观的好方法来做我想做的事情,这
I need to iterate over a vector from the end to the beginning. The "correct" way is
for(std::vector<SomeT>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit)
{
//do Something
}
When //do Something
involves knowing the actual index, then some calculations need to be done with rit
to obtain it, like index = v.size() - 1 - (rit - v.rbegin)
If the index is needed anyway, then I strongly believe it is better to iterate using that index
for(int i = v.size() - 1; i >= 0; --i)
{
//do something with v[i] and i;
}
This gives a warning that i
is signed and v.size()
is unsigned.
Changing to
for(unsigned i = v.size() - 1; i >= 0; --i)
is just functionally wrong, because this is essentially an endless loop :)
What is an aesthetically good way to do what I want to do which
- is warning-free
- doesn't involve casts
- is not overly verbose
发布评论
评论(11)
正如您所注意到的,当无符号时,
i >= 0
条件的问题在于该条件始终为真。不要在初始化i
时减去 1,然后在每次迭代后再次减去 1,而是在检查循环条件后减去 1:我喜欢这种样式有几个原因:
i
会环绕到循环结束时的UINT_MAX
,它不依赖这种行为——如果类型被签名,它的工作原理是一样的。依赖未签名的环绕对我来说感觉有点像黑客。size()
一次。>=
。每当我在 for 循环中看到该运算符时,我都必须重新读取它以确保不存在相差一错误。As you've noted, the problem with a condition of
i >= 0
when it's unsigned is that the condition is always true. Instead of subtracting 1 when you initializei
and then again after each iteration, subtract 1 after checking the loop condition:I like this style for several reasons:
i
will wrap around toUINT_MAX
at the end of the loop, it doesn't rely on that behavior — it would work the same if the types were signed. Relying on unsigned wraparound feels like a bit of a hack to me.size()
exactly once.>=
. Whenever I see that operator in afor
loop, I have to re-read it to make sure there isn't an off-by-one error.没有什么可以阻止您的
reverse_iterator
循环也使用索引,如多个其他答案中所述。这样,您就可以根据需要在// do the work
部分中使用迭代器或索引,以最小的额外成本。虽然我很想知道你需要索引做什么。访问
v[index]
与访问*rit
相同。There's nothing to stop your
reverse_iterator
loop also using the index as described in multiple other answers. That way you can use the iterator or index as needed in the// do the work
part, for minimal extra cost.Though I'm curious to know what you need the index for. Accessing
v[index]
is the same as accessing*rit
.美观! ;)
to be aesthetically pleasing! ;)
在 C++20 中,可以使用范围 (
#include
),或者如果需要保存在不同的变量中。
In C++20 one can use ranges (
#include <ranges>
)or if it is required to save in a different variable.
我更喜欢反向迭代器变体,因为它仍然很容易解释并且可以避免与索引相关的错误。
有时您可以简单地使用
BOOST_REVERSE_FOREACH
,这将使您的代码请看以下方式:实际上,您始终可以对此类循环使用
foreach
语句,但随后它们会变得有点不明显:I would prefer the reverse iterator variant, because it's still easy to interpret and allows to avoid index-related errors.
Sometimes you can simply use the
BOOST_REVERSE_FOREACH
, which would make your code look the following way:Actually speaking, you can always use
foreach
statements for these kinds of loops, but then they become a bit unobvious:尝试一下:
Try out a do while :
嗨,我认为更好的方法是使用迭代器,就像在第一个示例中使用的那样,如果您需要获取迭代器索引,则可以使用
std::distance 来计算它,如果我理解你的问题
Hi i think better way use iterator as you use in first sample and if you need get iterator index you can use
std::distance to calculate it, if i understand your question
循环条件
i != std::numeric_limits::max()
...或者使用UINT_MAX
如果您认为它很冗长。或另一种方式:
for(unsigned j=0, end=v.size(), i=end-1; j
或
for(unsigned end=v.size(), i=end-1; (end-i)
loop condition
i != std::numeric_limits<unsigned>::max()
... or useUINT_MAX
if you think its to verbose.or another way:
for(unsigned j=0, end=v.size(), i=end-1; j<end; --i, ++j)
or
for(unsigned end=v.size(), i=end-1; (end-i)<end; --i)
如果你的向量不是很大,你可以将大小转换为 int
You can cast the size to an int if your vector is not huge
我认为:
如果你早点检查就好了
。
I think that:
is fine if you check
earlier.
“转到”运算符肯定会扰乱我的头。
The "goes to" operator definitely messes with my head.