修改地图中的队列

时间:2013-01-31 07:11:22

标签: c++ map queue const

无法修改地图中的队列。

map<string , queue<item*> > itemList; // what the map creation looks like

map<string, queue<item*> >::const_iterator itr; // creating an iterator

//for every item in a warehouse
for(itr = itemList.begin(); itr != itemList.end(); ++itr)
{
    //while there are items in the queue with 1 day shelf life
    while(itr->second.front()->getLife() == 1)
    {
        //throw them away
        itr->second.pop();
    }
}

但是我一直在告诉我这个错误:

错误:传递'const std :: queue&gt; &gt;''作为'std :: queue&gt;的'this'参数&GT;&安培; std :: queue&gt; &gt; :: operator =(const std :: queue&gt;&gt;&amp;)'丢弃限定符

提前感谢您对此的任何帮助。 : - (

1 个答案:

答案 0 :(得分:3)

您正在通过const_iterator访问地图元素,因此您无法修改它们(严格来说,您只能在元素上调用const方法,而std::queue::pop()不是一个)。请尝试使用非const iterator

map<string, queue<item*> >::iterator itr;