Rella Rella - 1 year ago 74
C++ Question

Is it possible to remove queue element by value?

I want to remove element from queue with specific value. How to do such thing? (I am trying to create a concurrent mixture of map and queue and currently I try to implement on this answer)

So I currently have such code:


#include <map>
#include <deque>
#include <boost/thread.hpp>
#include <boost/thread/locks.hpp>

template <class map_t_1, class map_t_2>
class concurrent_queued_map
std::map<map_t_1, map_t_2> _ds;
std::deque<map_t_1> _queue;
mutable boost::mutex mut_;
concurrent_queued_map() {}

map_t_2 get(map_t_1 key) {
boost::mutex::scoped_lock lock(mut_);
return _ds[key];

map_t_1 put(map_t_1 key, map_t_2 value) {
boost::mutex::scoped_lock lock(mut_);
_ds.insert(std::pair<map_t_1, map_t_2>(key,value));
return key;

map_t_2 get_last(map_t_1 key) {
boost::mutex::scoped_lock lock(mut_);
const map_t_1 k = _queue.front();
return _ds[k];

void remove_last(map_t_1 key) {
boost::mutex::scoped_lock lock(mut_);
const map_t_1 k = _queue.front();

void remove(map_t_1 key) {
boost::mutex::scoped_lock lock(mut_);
_queue.erase(std::remove(_queue.begin(), _queue.end(), key), _queue.end());

int size() {
boost::mutex::scoped_lock lock(mut_);
return _ds.size();



So what shall I do? How to remove from queue by value? Or thare is any STL or Boost component that is alike queue? Meaning it would have
and also support search and erase by value?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download