C++中如何遍历queue容器?
不可以。
'queue'是一个容器适配器(container adapter),底层实现使用的是'deque'(双端队列),因此无法使用范围for循环进行遍历。
需要使用while循环和'front()'、'pop()'等函数来遍历'queue'中的元素。例如:
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
while (!q.empty()) {
int front = q.front();
q.pop();
// 处理队首元素
}
原文地址: https://www.cveoy.top/t/topic/ovxY 著作权归作者所有。请勿转载和采集!