简体   繁体   English

双端队列迭代器不可递减

[英]Deque iterator not decrementable

I try to run this code with MSVC 2017: 我尝试使用MSVC 2017运行以下代码:

#include <vector>
#include <deque>

class StripPtR {
public:
    int i;
    StripPtR (int i) : i(i) {}
};

typedef std::deque<StripPtR> StripType;
typedef std::vector<StripType> StripsType;

int main(int, char**) {
    StripType a{ {1}, {2}, {3} };

    a.insert(a.end(), a.rbegin() + 1, a.rend());

    return 0;
}

And I get this error: 我得到这个错误:

Deque iterator not decrementable 双端队列迭代器不可递减

Error 错误

The error occurs at runtime. 该错误在运行时发生。 During compile time there is no error or warning. 在编译期间,没有错误或警告。

The same code works fine with GCC. 相同的代码可以在GCC上正常工作。

What is wrong? 怎么了?

std::deque::insert says: std::deque::insert说:

All iterators, including the past-the-end iterator, are invalidated. 所有迭代器(包括过去的迭代器)均无效。

The MSVC version probably loops through, incrementing/decrementing first (or a.rbegin() + 1 in your code) which means that the insert works but afterwards these iterators are invalidated and cause your runtime error. 在MSVC版本可能会循环,增大/减小first (或a.rbegin() + 1在你的代码),这意味着插入的作品,但后来这些迭代器失效,并且会导致运行错误。 This version results in that you can't pass iterators to the same container you insert into. 此版本导致无法将迭代器传递到insert容器中。

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

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