I need to be able to add (append) the contents of one std::queue to another, preferably in much the same way as using std::deque::insert, but using std::vector? I would prefer to continue using std::vectors, rather than a major rewrite to implement std::deques.
As with some of my previous posts in this same project, I have limited mobility in terms of what I can use having to work with some legacy code. And much of that limitation has to do with speed. Nevertheless, members of this forum have come up with some elegant and unique solutions; and I'm hoping to find another.
// This works...
std::deque<std::vector<uint8_t>> aaa;
std::deque<std::vector<uint8_t>> bbb;
aaa.insert(aaa.end(), bbb.begin(), bbb.end());
// This, of course, does not work...
std::queue<std::vector<uint8_t>> ccc;
std::queue<std::vector<uint8_t>> ddd;
ccc.insert(ccc.end(), ddd.begin(), ddd.end());
Obviously will not compile as insert is not supported for ccc type std::queue
A few important notes: There will never be a case where the container being used (queue, deque, etc) will need anything other than FIFO. Also, the processing handles queue volumes in the range of 80,000 to 100,000 elements per second, often very small, occasionally very large.
std::queue
is (by default) an adapter on top of std::deque
, and one specifically designed to remove operations not associated with FIFO queue structure.
Inserting series of elements, in particular, is not something which is defined for FIFO container, and the whole reason of using std::queue
in the first place is to restrict operations logically not part of the FIFO container interface.
Having said that, std::queue
exposes the underlying container it uses as a protected member variable C
, so one way you can workaround this limitation is by inheriting your custom adapter from std::deque
and exposing underlying container.
However, that somewhat defeats the purpose, and may be you'd be better off starting directly with std::deque
?