Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::queue iteration

I need to iterate over std::queue. www.cplusplus.com says:

By default, if no container class is specified for a particular queue class, the standard container class template deque is used.

So can I somehow get to the queue's underlying deque and iterate over it?

like image 318
jackhab Avatar asked Aug 11 '09 08:08

jackhab


People also ask

Does STD queue have iterator?

Using std::queue In other words, std::queue is not meant to be iterated over. If you need to iterate over a std::queue , you can create a copy of it and remove items from the copy, one at a time, using the standard pop function after processing it.

Can you iterate over queue?

Implement your queue as a LinkedList. Then you can iterate over your objects in order they were inserted. You have to declare the type of object being insert into the queue so you won't get any errors. You can keep it as object and specify it as a queue of objects then your code above would work.

Can we iterate over priority queue C++?

C++ priority_queue does not offer a . begin() pointer (like vector would do) that you can use to iterate over it. If you want to iterate over the priority queue to search for whether it contains a value then maybe create a wrapper priority queue and use a hash set to keep track of what you have in the queue.

How do you iterate through a STD list?

Create an iterator of std::list. Point to the first element. Keep on increment it, till it reaches the end of list. During iteration access, the element through iterator.


4 Answers

If you need to iterate over a queue then you need something more than a queue. The point of the standard container adapters is to provide a minimal interface. If you need to do iteration as well, why not just use a deque (or list) instead?

like image 190
CB Bailey Avatar answered Oct 28 '22 20:10

CB Bailey


While I agree with others that direct use of an iterable container is a preferred solution, I want to point out that the C++ standard guarantees enough support for a do-it-yourself solution in case you want it for whatever reason.

Namely, you can inherit from std::queue and use its protected member Container c; to access begin() and end() of the underlying container (provided that such methods exist there). Here is an example that works in VS 2010 and tested with ideone:

#include <queue>
#include <deque>
#include <iostream>

template<typename T, typename Container=std::deque<T> >
class iterable_queue : public std::queue<T,Container>
{
public:
    typedef typename Container::iterator iterator;
    typedef typename Container::const_iterator const_iterator;

    iterator begin() { return this->c.begin(); }
    iterator end() { return this->c.end(); }
    const_iterator begin() const { return this->c.begin(); }
    const_iterator end() const { return this->c.end(); }
};

int main() {
    iterable_queue<int> int_queue;
    for(int i=0; i<10; ++i)
        int_queue.push(i);
    for(auto it=int_queue.begin(); it!=int_queue.end();++it)
        std::cout << *it << "\n";
    return 0;
}
like image 29
Alexey Kukanov Avatar answered Oct 28 '22 19:10

Alexey Kukanov


you can save the original queue to a temporary queue. Then you simply do your normal pop on the temporary queue to go through the original one, for example:

queue tmp_q = original_q; //copy the original queue to the temporary queue

while (!tmp_q.empty())
{
    q_element = tmp_q.front();
    std::cout << q_element <<"\n";
    tmp_q.pop();
} 

At the end, the tmp_q will be empty but the original queue is untouched.

like image 20
StupidMe Avatar answered Oct 28 '22 19:10

StupidMe


One indirect solution can be to use std::deque instead. It supports all operations of queue and you can iterate over it just by using for(auto& x:qu). It's much more efficient than using a temporary copy of queue for iteration.

like image 6
Tejas Patil Avatar answered Oct 28 '22 21:10

Tejas Patil