简体   繁体   中英

Deque iterator not decrementable

I try to run this code with 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.

What is wrong?

std::deque::insert says:

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. This version results in that you can't pass iterators to the same container you insert into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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