修改映射中的队列

modifying a queue in a map

本文关键字:队列 映射 修改      更新时间:2023-10-16

修改地图中的队列时遇到问题。

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>>"作为"std::queue>>&std::queue>>::

operator=(const std::queue>>&)"的"this"参数传递丢弃限定符

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

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

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