#include <stdio.h> #include <queue> void dumpStringQueue(std::queue<std::string> &queueIn) { std::queue<std::string> tmpQueue = queueIn; // Make a copy printf("--- Queue Dump (%d items) ---\n", (int)tmpQueue.size()); while(!tmpQueue.empty()) { const std::string front = tmpQueue.front(); printf("%s\n", front.c_str()); tmpQueue.pop(); } printf("--- End Queue Dump ---\n"); } void exampleUse() { std::queue<std::string> myQueue; myQueue.push("one"); myQueue.push("two"); myQueue.push("three dumpStringQueue(myQueue); }
Programming Tips - C++: dump a std::queue of std::string's
Date: 2025may13
Language: C++
Q. C++: dump a std::queue of std::string's
A. std::queue doesn't implement iterators so we make a copy
of the queue and repeatedly use front() and pop() on the copy.
(I saw another implementation that did this on the original queue
and then restored it which seems ill-advised to me.)
Of course, making a copy could be slow for a large queue.