简体   繁体   English

递减 std::vector::begin 是否未定义,即使它从未使用过?

[英]Is decrementing std::vector::begin undefined, even if it is never used?

Note that, contrary to the many questions on the subject (and probably why I can not find a satisfactory answer to this question neither on google nor on stackoverflow), I never dereference *(begin() - 1)请注意,与关于该主题的许多问题相反(以及为什么我在 google 和 stackoverflow 上都找不到这个问题的满意答案),我从不取消引用*(begin() - 1)


My requirements are to:我的要求是:

  • iterate backwards向后迭代
  • use functions that do not take reverse iterators, in this example vector::erase()使用不采用反向迭代器的函数,在本例中为vector::erase()
  • try to keep the code clean, so try to avoid the mental juggling of:尽量保持代码干净,所以尽量避免心理上的杂耍:

    vector.erase(rev_it.base() - 1)

    (What should the reverse iterator be now to get the the next element in the iteration ? The iterator returned by erase() ? + 1 , probably ? - 1 , unlikely ?) (反向迭代器现在应该是什么来获取迭代中的下一个元素? erase()返回的迭代器? + 1 ,可能是? - 1 ,不太可能?)

What I've come up with is:我想出的是:

for (auto it = vector.end(); it-- != vector.begin(); ) {
    if (condition(*it)) {
        it = vector.erase(it);
    }
}

This seems to work, as it-- returns the iterator's value and then only decrements it, meaning the iterator is always decremented after the check but before entering the loop body .这似乎有效,因为it--返回迭代器的值,然后只递减它,这意味着迭代器总是在检查之后在进入循环体之前递减。

In particular:特别是:

When entering the loop进入循环时

  • if vector.end() == vector.begin() the vector is empty and we exit the loop immediately如果vector.end() == vector.begin()向量为空,我们立即退出循环
  • if vector.end() != vector.begin() then we enter the loop, with the first loop body execution with it == vector.end() - 1 if vector.end() != vector.begin()然后我们进入循环,第一个循环体执行it == vector.end() - 1

When erasing elements擦除元素时

vector.erase(it) returns the following element in the vector, so decrementing the iterator at every iteration gets us to consider exactly once every element in the vector. vector.erase(it)返回向量中的以下元素,因此在每次迭代时递减迭代器让我们只考虑向量中的每个元素一次。

When exiting the loop退出循环时

At the last execution of the loop's body, it == vector.begin() , so the next time we try the loop condition:在最后一次执行循环体时, it == vector.begin() ,所以下次我们尝试循环条件时:

  • the condition returns false条件返回false
  • it is decremented one last time it递减最后一次
  • we exit the loop我们退出循环

That is, my code does seem to compute the iterator position begin() - 1 but never accesses it , nor uses it for comparisons or anything like that.也就是说,我的代码似乎确实计算了迭代器位置begin() - 1但从不访问它也不使用它进行比较或类似的操作。

Is this undefined behaviour?这是未定义的行为吗?

Do I risk a segfault or something?我是否冒着段错误的风险? Or just accessing uninitialized data maybe?或者只是访问未初始化的数据? Nothing at all because the iterator is discarded before being used in anyway?什么都没有,因为迭代器在使用之前就被丢弃了? Is there no way of knowing?没有办法知道吗?

How about怎么样

for (auto it = vector.end(); it != vector.begin(); ) {
    --it;
    ... rest of the loop body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM