Range-based loop for std::queue

Viewed 13384

I'm trying to look for a substitute in std::vector in my project, and I found out that std::queue is what I'm looking for.

I have lots of function that uses range-based loop for iteration and I'm trying to maintain it as far as I can.

I try to compile a range-based loop in std::queue but all I get are compile errors

error: no matching function for call to 'begin(std::queue&)'

Doesn't std::queue support range base loop?

I did try Google search but didn't find any topic regarding to this.

Update:

My compiler is GCC v4.7.1

-std=c++11 is enabled

And here's the faulty test code:

std::queue<int> Q;

for (int i = 0;i < 10; ++i)
    Q.push(i);

std::cout << "\nqueue contains: ";
for (auto i : Q)
    std::cout << i << ", ";
2 Answers

Although std::queue doesn't support iteration, it does have a copy constructor which allows us to transfer the contents of a temporary copy of the queue to another temporary container that would support iteration (e.g. std::vector, std::list, or if using C++11, std::forward_list).

So, while it does cost some runtime overhead and isn't quite as nice as being able to iterate over the structure directly if the goal is to have a reasonably convenient way to iterate over the values of a std::queue variable, you might consider using a templated function like the following (adding it to an appropriate header file):

#include <queue>
#include <list>

// returns an iterable copy of the given queue
template<class T> std::list<T> toList(std::queue<T> qCopy) {
    std::list<T> returnList;
    while (!qCopy.empty()) {
        returnList.push_back(qCopy.front());
        qCopy.pop();
    }
    return returnList;
}

Incorporating into OP's use case:

#include <iostream>
int main() {
    std::queue<int> Q;

    for (int i = 0;i < 10; ++i)
        Q.push(i);

    std::cout << "\nqueue contains: ";
    for (auto i : toList(Q))
        std::cout << i << ", ";

    return 0;
}
Related