遍历C++ STL队列的解决方法如下所示:
#include
#include
int main() {
std::queue myQueue;
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
myQueue.push(4);
// 使用while循环和.front()方法来遍历队列
while (!myQueue.empty()) {
std::cout << myQueue.front() << " ";
myQueue.pop();
}
std::cout << std::endl;
return 0;
}
上述示例代码创建了一个整数类型的队列,然后依次插入了1、2、3和4。接下来使用while循环和.front()方法来遍历队列,打印出队列中的每个元素,并在遍历过程中调用.pop()方法来删除已经访问过的元素。最终输出结果为:1 2 3 4。
上一篇:遍历不遵循列表的值
下一篇:遍历C++对象数组。