【问题标题】:modifying a queue in a map修改地图中的队列
【发布时间】:2013-01-31 07:11:22
【问题描述】:

无法修改地图中的队列。

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' 参数传递丢弃限定符

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

【问题讨论】:

    标签: c++ map queue constants


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-28
      • 2020-11-17
      • 2019-11-10
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      相关资源
      最近更新 更多