简体   繁体   English

向量迭代器不递减?

[英]Vector Iterator not decrementable?

Can some one shed some light on why this code snipit will run though the loop once but then give an assertion failure Expression: vector iterator not decrementable? 有人可以阐明为什么此代码伪指令将通过循环运行一次却又导致断言失败的原因表达式:向量迭代器不可递减吗?

for (auto an = a.rbegin(); an != a.rend(); ++an, indexA--) //first number 
{
        for (auto bn = b.rbegin(); bn != b.rend(); ++bn) //second number
        {
            if (*an - *bn >= 0)
            {
                returnVal.push_back(*an - *bn);
                a.pop_back();
                b.pop_back();
            }
            else
            {
                brrow = *an + 10;
                a.at(indexA - 1) = a.at(indexA - 1) - 1; // remove 1 from the spot ahead of current digit
                returnVal.push_back(brrow - *bn);
                a.pop_back();
                b.pop_back();
            }
        }   
}

Based on your comments, it seems you want to move through both iterator ranges in tandem - that only needs one for loop, like this: 根据您的评论,您似乎想一并遍历两个迭代器范围-仅需要一个for循环,如下所示:

auto an = a.rbegin();
for (auto bn = b.rbegin();
     an != a.rend() && bn != b.rend();
     ++an, ++bn, indexA--)
    if (*an - *bn >= 0)
        returnVal.push_back(*an - *bn);
    else
    {
        brrow = *an + 10;
        --(a.at(indexA - 1)); // remove 1 from the spot ahead of current digit
                              // shouldn't you check for rend() first????
        returnVal.push_back(brrow - *bn);
    }

Note that both iterators can be declared as for -loop local if you know they'll be of the exact same type: 请注意, 如果您知道两个迭代器的类型完全相同,则可以将它们声明for -loop local:

for (auto an = a.rbegin(), bn = b.rbegin(); ...

pop_back() will make your iterator invalid. pop_back()将使您的迭代器无效。

Following explains more in detail ( Does pop_back() really invalidate *all* iterators on an std::vector? ): 以下内容进行了更详细的说明( pop_back()是否真的会使std :: vector上的* all *迭代器无效? ):

Here is your answer, directly from The Holy Standard: 这是您的回答,直接来自圣典:

23.2.4.2 A vector satisfies all of the requirements of a container and of a reversible container (given in two tables in 23.1) and of a sequence, including most of the optional sequence requirements (23.1.1). 23.2.4.2向量满足容器和可逆容器(在23.1中的两个表中给出)和序列的所有要求,包括大多数可选的序列要求(23.1.1)。

23.1.1.12 Table 68 expressiona. 23.1.1.12表68表达式 pop_back() return typevoid operational semanticsa. pop_back()返回没有操作语义的类型。 erase(--a.end()) containervector, list, deque 擦除(--a.end())容器向量 ,列表,双端队列

Notice that a.pop_back is equivalent to a.erase(--a.end()). 请注意,a.pop_back等效于a.erase(-a.end())。 Looking at vector's specifics on erase: 查看擦除时向量的详细信息:

23.2.4.3.3 - iterator erase(iterator position) - effects - Invalidates all the iterators and references after the point of the erase 23.2.4.3.3-迭代器擦除(迭代器位置)-效果- 使擦除点之后的所有迭代器和引用无效

Therefore, once you call pop_back, any iterators to the previously final element (which now no longer exists) are invalidated. 因此,一旦调用pop_back,到先前最后一个元素(现在不再存在)的所有迭代器都将失效。


More information: FAQ "Iterator invalidation rules": Iterator invalidation rules 详细信息:FAQ“迭代器无效规则”: 迭代器无效规则

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

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