C++ 反向迭代向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4205720/
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
Iterating over a vector in reverse direction
提问by Armen Tsirunyan
I need to iterate over a vector from the end to the beginnig. The "correct" way is
我需要从结尾到开头迭代一个向量。“正确”的方式是
for(std::vector<SomeT>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit)
{
//do Something
}
When doSomething 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)
当 doSomething 涉及知道实际索引时,则需要使用 rit 进行一些计算才能获得它,例如 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
这给出了一个i
已签名和v.size()
未签名的警告。更改为
for(unsigned i = v.size() - 1; i >= 0; --i)
is just functionally wrong, because this is essentially an endless loop :)
for(unsigned i = v.size() - 1; i >= 0; --i)
只是在功能上是错误的,因为这本质上是一个无限循环 :)
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
- 无警告
- 不涉及演员表
- 不会过于冗长
I hope I am not looking for something that doesn't exist :)
我希望我不是在寻找不存在的东西:)
回答by Rob Kennedy
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 initialize i
and then again after each iteration, subtract 1 after checking the loop condition:
正如您所指出的,i >= 0
无符号条件的问题在于条件始终为真。不是在初始化时减去 1 i
,然后在每次迭代后再次减去,而是在检查循环条件后减去 1:
for (unsigned i = v.size(); i-- > 0; )
I like this style for several reasons:
我喜欢这种风格有几个原因:
- Although
i
will wrap around toUINT_MAX
at the end of the loop, it doesn't relyon 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. - It calls
size()
exactly once. - It doesn't use
>=
. 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. - If you change the spacing in the conditional, you can make it use the "goes to" operator.
- 尽管
i
将UINT_MAX
在循环结束时回绕到,但它不依赖于该行为——如果类型被签名,它的工作方式相同。依赖未签名的环绕对我来说有点像黑客。 - 它只调用
size()
一次。 - 它不使用
>=
. 每当我在for
循环中看到该运算符时,我都必须重新阅读它以确保没有逐一错误。 - 如果更改条件中的间距,则可以使用“goes to”运算符。
回答by Steve Townsend
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.
reverse_iterator
使用其他多个答案中所述的索引也无法阻止您的循环。这样您就可以根据需要在// do the work
部件中使用迭代器或索引,以最小的额外成本。
size_t index = v.size() - 1;
for(std::vector<SomeT>::reverse_iterator rit = v.rbegin();
rit != v.rend(); ++rit, --index)
{
// do the work
}
Though I'm curious to know what you need the index for. Accessing v[index]
is the same as accessing *rit
.
尽管我很想知道您需要索引的用途。访问v[index]
与访问相同*rit
。
回答by Nim
to be aesthetically pleasing! ;)
要美观!;)
for(unsigned i = v.size() - 1; v.size() > i; --i)
回答by Yippie-Ki-Yay
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:
有时您可以简单地使用BOOST_REVERSE_FOREACH
,这将使您的代码如下所示:
reverse_foreach (int value, vector) {
do_something_with_the_value;
}
Actually speaking, you can always use foreach
statements for these kinds of loops, but then they become a bit unobvious:
实际上,foreach
对于这些类型的循环,您始终可以使用语句,但是它们变得有点不明显:
size_t i = 0;
foreach (int value, vector) {
do_something;
++i;
}
回答by Jean-Bernard Jansen
Try out a do while :
试一试:
std::vector<Type> v;
// Some code
if(v.size() > 0)
{
unsigned int i = v.size() - 1;
do
{
// Your stuff
}
while(i-- > 0);
}
回答by Sanja Melnichuk
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
嗨,我认为更好的方法是使用迭代器,就像您在第一个示例中使用的那样,如果您需要获取迭代器索引,您可以使用 std::distance 来计算它,如果我理解您的问题
回答by smerlin
loop condition i != std::numeric_limits<unsigned>::max()
... or use UINT_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)
循环条件i != std::numeric_limits<unsigned>::max()
...或者UINT_MAX
如果您认为它很冗长,请使用。或其他方式:for(unsigned j=0, end=v.size(), i=end-1; j<end; --i, ++j)
或for(unsigned end=v.size(), i=end-1; (end-i)<end; --i)
回答by Kylo
I think that:
我觉得:
for(unsigned i = v.size() - 1; i >= 0; --i)
is fine if you check
如果你检查就好了
!v.empty()
earlier.
早些时候。
回答by Peter Arandorenko
for (it = v.end()-1; it != v.begin()-1; --it)
{
}
The "goes to" operatordefinitely messes with my head.
该“变为”运营商肯定有我的头混乱。