mr5
mr5

Reputation: 3580

Range-based loop for std::queue

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 << ", ";

Upvotes: 16

Views: 16270

Answers (2)

teichert
teichert

Reputation: 4693

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;
}

Upvotes: 2

Morwenn
Morwenn

Reputation: 22552

Well, the answer is actually pretty simple: there is no function begin() in std::queue and there isn't any overload of std::begin taking a std::queue either. You can have a look at the documentation.

The core problem is that std::queue is not meant to be iterated over. It exists to satisfy other needs. If you really need to iterate over it, you should just use the underlying container (by default std::deque) which supports iteration and for which your code would be valid.

Upvotes: 20

Related Questions