This throws when trying to remove element from deque
via iterator. The error is "can not seek value-initialized iterator" using VS2017. I wonder why this is happening, isn't std::deque
a doubly linked list that does not invalidate iterators on push_front()
/ push_back()
?
class deque2 {
public:
bool enqueue(int val) {
if (mp.find(val) != mp.end()) {
return false;
}
dq.push_front(val);
mp[val] = dq.begin();
return true;
}
int dequeue() {
if (dq.size() == 0) {
return -1;
}
int res = dq.back();
mp.erase(res);
dq.pop_back();
return res;
}
void erase(int val) {
auto it = mp.find(val);
if (it != mp.end()) {
dq.erase(it->second); // exception
mp.erase(val);
}
}
private:
deque<int> dq;
unordered_map<int, deque<int>::iterator> mp;
};
should have been used here. https://stackoverflow.com/questions/6292332/what-really-is-a-deque-in-stl has the internal explanation how deque works in STL under the hood
– dgrandm Aug 10 '20 at 23:10