反向迭代向量

发布于 2024-10-03 11:24:10 字数 733 浏览 4 评论 0 原文

我需要从末尾到开头迭代一个向量。 “正确”的方法是

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(11

停顿的约定 2024-10-10 11:24:10

正如您所注意到的,当无符号时,i >= 0 条件的问题在于该条件始终为真。不要在初始化 i 时减去 1,然后在每次迭代后再次减去 1,而是在检查循环条件后减去 1:

for (unsigned i = v.size(); i-- > 0; )

我喜欢这种样式有几个原因:

  • 虽然 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 initialize i and then again after each iteration, subtract 1 after checking the loop condition:

for (unsigned i = v.size(); i-- > 0; )

I like this style for several reasons:

  • Although i will wrap around to UINT_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.
  • It calls size() exactly once.
  • It doesn't use >=. Whenever I see that operator in a for 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.
枕花眠 2024-10-10 11:24:10

没有什么可以阻止您的 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
}

虽然我很想知道你需要索引做什么。访问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.

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.

那些过往 2024-10-10 11:24:10

美观! ;)

for(unsigned i = v.size() - 1; v.size() > i; --i)

to be aesthetically pleasing! ;)

for(unsigned i = v.size() - 1; v.size() > i; --i)
山色无中 2024-10-10 11:24:10

在 C++20 中,可以使用范围 (#include ),

//DATA
std::vector<int> vecOfInts = { 2,4,6,8 };

//REVERSE VECTOR 
for (int i : vecOfInts | std::views::reverse)
{
     std::cout << i << "  ";
} 

或者如果需要保存在不同的变量中。

//SAVE IN ANOTHER VARIABLE
auto reverseVecOfInts = std::views::reverse(vecOfInts);

//ITERATION
for (int i : reverseVecOfInts)
{
    std::cout << i << "  ";
}

In C++20 one can use ranges (#include <ranges>)

//DATA
std::vector<int> vecOfInts = { 2,4,6,8 };

//REVERSE VECTOR 
for (int i : vecOfInts | std::views::reverse)
{
     std::cout << i << "  ";
} 

or if it is required to save in a different variable.

//SAVE IN ANOTHER VARIABLE
auto reverseVecOfInts = std::views::reverse(vecOfInts);

//ITERATION
for (int i : reverseVecOfInts)
{
    std::cout << i << "  ";
}
偏闹i 2024-10-10 11:24:10

我更喜欢反向迭代器变体,因为它仍然很容易解释并且可以避免与索引相关的错误。

有时您可以简单地使用 BOOST_REVERSE_FOREACH,这将使您的代码请看以下方式:

reverse_foreach (int value, vector) {
   do_something_with_the_value;
}

实际上,您始终可以对此类循环使用 foreach 语句,但随后它们会变得有点不明显:

size_t i = 0;

foreach (int value, vector) {
   do_something;
   ++i;
}

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:

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:

size_t i = 0;

foreach (int value, vector) {
   do_something;
   ++i;
}
友谊不毕业 2024-10-10 11:24:10

尝试一下:

std::vector<Type> v;
// Some code 
if(v.size() > 0)
{
    unsigned int i = v.size() - 1;
    do
    {
        // Your stuff
    }
    while(i-- > 0);
}

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);
}
烛影斜 2024-10-10 11:24:10

嗨,我认为更好的方法是使用迭代器,就像在第一个示例中使用的那样,如果您需要获取迭代器索引,则可以使用
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

没︽人懂的悲伤 2024-10-10 11:24:10

循环条件 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 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)

反差帅 2024-10-10 11:24:10

如果你的向量不是很大,你可以将大小转换为 int

for (int i = int(v.size()) - 1; i >= 0; --i) {
    //do something with v[i] and i; 
}

You can cast the size to an int if your vector is not huge

for (int i = int(v.size()) - 1; i >= 0; --i) {
    //do something with v[i] and i; 
}
肥爪爪 2024-10-10 11:24:10

我认为:

for(unsigned i = v.size() - 1; i >= 0; --i)

如果你早点检查就好了

!v.empty()

I think that:

for(unsigned i = v.size() - 1; i >= 0; --i)

is fine if you check

!v.empty()

earlier.

野の 2024-10-10 11:24:10
for (it = v.end()-1; it != v.begin()-1; --it)
{
}

“转到”运算符肯定会扰乱我的头。

for (it = v.end()-1; it != v.begin()-1; --it)
{
}

The "goes to" operator definitely messes with my head.

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