Reputation: 478
I am trying to copy a std::vector
into std::priority_queue
while the following two approaches are working.
vector<int> parts = { 8, 4, 6, 12, 57, 28}; //source
priority_queue<int, vector<int>, greater<int>> minHeap; //target
approach 1:
all_of(parts.begin(), parts.end(), [&minHeap](int& part){minHeap.push(part); return true;});
approach 2:
for(int& part: parts) minHeap.push(part);
However, I'd like to know if the same can be done by std::copy
?
Upvotes: 1
Views: 379
Reputation: 596287
Yes, it can be done with std::copy()
, but only if you write (or find) a custom OutputIterator
that calls the queue's push()
method when the iterator is written to. The standard library has no such iterator built-in for calling push()
(but it has std::front_insert_iterator
for push_front()
, std::back_insert_iterator
for push_back()
, std::insert_iterator
for insert()
, etc).
Upvotes: 3