Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当erase无法删除迭代器指向的元素时,该怎么办?_C++_List_Erase - Fatal编程技术网

C++ 当erase无法删除迭代器指向的元素时,该怎么办?

C++ 当erase无法删除迭代器指向的元素时,该怎么办?,c++,list,erase,C++,List,Erase,在下面的代码中,我尝试删除templist的一个特定元素。但是,仅删除列表的最后一个元素。如何删除该特定元素 for(index1 = templist.begin(); index1 != templist.end();) { checkit=templist.end(); --checkit; if((*index1).origin == (*udit).dest && sumweight + (*index1).weight <= 25000

在下面的代码中,我尝试删除templist的一个特定元素。但是,仅删除列表的最后一个元素。如何删除该特定元素

for(index1 = templist.begin(); index1 != templist.end();)
{
    checkit=templist.end();
    --checkit;

    if((*index1).origin == (*udit).dest && sumweight + (*index1).weight <= 25000)
    {
        sumhr += 1 + (*udit).hr;
        sumweight = sumweight + (*index1).weight;
        stops++;

        tour.at(i).push_back((*index1));

        if(index1! = checkit)
            index1 = templist.erase(index1);
        else
        {
            templist.erase(index1);
            index1 = templist.end();
        }
    }
    else
        index1++;
}
for(index1=templast.begin();index1!=templast.end();)
{
checkit=templast.end();
--检查它;

if((*index1.origin==(*udit.dest&&sumweight+(*index1).weight您的问题是,从容器中删除元素后,迭代器无效。要解决此问题,您只需稍微更改一下逻辑即可。由于
erase
函数返回一个迭代器,该迭代器引用容器中的下一个元素,因此您可以使用该迭代器。您在项目中具体如何执行此操作由您决定,但它应该如下所示

if (index1 != checkit)
{
    // Remove the item. The iterator returned by "erase" is the next one
    // in line so there's no need to manually advance to the iterator with ++
    index1 = templist.erase(index1);
}
else
{
    // Remove the item and skip to the end.
    templist.erase(index1);
    index1 = templist.end();
}
你问:

当erase无法删除迭代器指向的元素时,该怎么办

不知道你是如何得出结论的。一些数据支持这一说法会很有用

但是,迭代器的使用有点问题。删除元素后,迭代器将递增两次

建议的解决方案:

for(index1=templist.begin(); index1!=templist.end(); /* Don't increment the iterator here */ ) 
{
   if((*index1).origin==(*udit).dest && sumweight + (*index1).weight <=25000)
   {
      sumhr+=1+(*udit).hr;                         
      sumweight=sumweight+(*index1).weight;
      stops++;

      tour.at(i).push_back((*index1));

      // Erase the item and get the next iterator.
      index1 = templist.erase(index1);
   }
   else
   {
      // Increment the iterator only when we are not erasing.
      ++index1;
   }
}
for(index1=templist.begin();index1!=templist.end();/*不要在此处递增迭代器*)
{

if(*index1.origin==(*udit.dest&&sumweight+(*index1.weight)不清楚您的问题是什么以及您的代码做了什么(或应该做什么)。您能否将代码的复杂性降低到足以说明问题的程度?这是:
templast.erase(index1);if(index1!=checkit)
不好,你刚刚擦除了它!然后这个:
index1++;
你在for循环减速和循环本身中增加了
index1
,你打算这样做吗?谢谢你的帮助,我实现了这些更改,但仍然只擦除了最后一个元素。我进一步上传了整个程序,也许缺陷就在那里。