C++ 遍历std队列

C++ 遍历std队列,c++,iterator,queue,foreach,C++,Iterator,Queue,Foreach,我试图使用BOOST_FOREACH来迭代std::队列。但该类中没有迭代器,因为我有一个错误: std::queue<std::string> someList; BOOST_FOREACH(std::string temp, someList) { std::cout << temp; } >no matching function for call to begin(...) >no type named ‘iterator’ in ‘class

我试图使用BOOST_FOREACH来迭代std::队列。但该类中没有迭代器,因为我有一个错误:

std::queue<std::string> someList;
BOOST_FOREACH(std::string temp, someList)
{
   std::cout << temp;
}

>no matching function for call to begin(...)
>no type named ‘iterator’ in ‘class std::queue<std::basic_string<char> >’
std::queue someList;
BOOST_FOREACH(std::string temp,someList)
{
std::无法为调用开始(…)提供匹配函数
>“class std::queue”中没有名为“iterator”的类型
我需要这样的结构:先来后走。

支持在数据结构的开头和结尾进行有效的插入和删除。您可以使用
push_back
pop_front
手动执行队列操作

默认情况下,队列在内部使用deque。它是一个只公开队列操作的包装器(因此不能对其进行迭代)。我问了一段时间,最好的答案让我很好地了解了
std::queue
的实际用途。应该使用
std::queue
不是因为需要队列,而是为了清楚地表明,在给定的数据结构上,只有类似队列的操作才是合法的。听起来你需要更多的自由,所以使用deque,list,或者在两端插入和移除O(1)的其他结构。

您可以使用
std::list(具有前推和后弹出功能)是一个容器适配器。它使用
std::deque
作为默认的基础容器。无法访问此容器,因此不会以任何方式进行迭代

最好的方法是使用
std::deque
std::list
并自己管理队列行为。可能提供自己的包装器。

可能的重复:我认为队列比deque更简单(perf+mem)。但在回答(并通过头文件验证)之后,我认为性能和内存没有区别,不管是队列还是deque,除了在队列的情况下对某些操作的限制。