Insert into an STL queue using std::copy
Unfortunately std::queue
'adapts' the function known as push_back
to just push
which means that the standard back_insert_iterator
doesn't work.
Probably the simplest way (albeit conceptually ugly) is to adapt the container adapter with a short lived container adapter adapter[sic] (eugh!) that lives as long as the back insert iterator.
template<class T>
class QueueAdapter
{
public:
QueueAdapter(std::queue<T>& q) : _q(q) {}
void push_back(const T& t) { _q.push(t); }
private:
std::queue<T>& _q;
};
Used like this:
std::queue<int> qi;
QueueAdapter< std::queue<int> > qiqa( qi );
std::copy( v.begin(), v.end(), std::back_inserter( qiqa ) );
Queue does not allow iteration through its elements.
From the SGI STL Docs:
A queue is an adaptor that provides a restricted subset of Container functionality A queue is a "first in first out" (FIFO) data structure. 1 That is, elements are added to the back of the queue and may be removed from the front; Q.front() is the element that was added to the queue least recently. Queue does not allow iteration through its elements. [2]
You can make this work, but you can't use insert_iterator
. You'll have to write something like queue_inserter
that presents an iterator interface.
Update I couldn't help myself and deicded to try to implement the iterator you need. Here are the results:
template< typename T, typename U >
class queue_inserter {
queue<T, U> &qu;
public:
queue_inserter(queue<T,U> &q) : qu(q) { }
queue_inserter<T,U> operator ++ (int) { return *this; }
queue_inserter<T,U> operator * () { return *this; }
void operator = (const T &val) { qu.push(val); }
};
template< typename T, typename U >
queue_inserter<T,U> make_queue_inserter(queue<T,U> &q) {
return queue_inserter<T,U>(q);
}
This works great for functions like this:
template<typename II, typename OI>
void mycopy(II b, II e, OI oi) {
while (b != e) { *oi++ = *b++; }
}
But it doesn't work with the STL copy because the STL is stupid.
std::queue
isn't a container in the STL sense, it's a container adapter with very limited functionality. For what you seem to need either std::vector
or std::deque
("double-ended queue, which is a "real container"), seems the right choice.