Is it possible to remove queue element by value?

泄露秘密 提交于 2019-12-05 04:59:43

A deque is a sequence container, so you can only remove elements by value, which is best done with the remove/erase idiom:

std::deque<T> q;
T val;

q.erase(std::remove(q.begin(), q.end(), val), q.end());

If you are using the std::queue adapter, then you cannot do this at all, because the adapter only exposes the front/back interface and is not intended for iteration or lookup semantics.

If you choose to implement your queue as an std::list, then use the member function remove() instead.

Dointg it like that - with both queue and map is removing advantages of using either, and keeps disadvantages of both (at least in terms of performace). I would simply use deque<std::pair<map_t_1, map_t_2> > to represent both map and deque. Then map lookup or editing requires looking through entire deque, so it's not very efficient.

More efficient solution would be more difficult to achieve, as you are trying to cope with two different indexing schemes - by key (map) and by index (required by ordering nature od deque). To do it efficiently i would propose self-implemented double-linked-list (as queue) with map of keys to linked_list entries. Double linked list entries would also contain key for lookup in the map when prepending/appending queue.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!