当列表重新分配内存时,我应该刷新list::迭代器吗

Should I refresh list::iterator when the list reallocates memory?

本文关键字:我应该 刷新 list 迭代器 内存 新分配 列表 分配      更新时间:2023-10-16

想象一条铁路线。我们有一些车站和路段,列车在那里减速行驶。这些区段可以包含工作站。我需要将这些部分拆分为不包含工作站的部分。

例如:从线路的1500米到3500米,列车只能以40公里/小时的速度行驶。我在2000米和3000米有两个车站。在这种情况下,我需要有三个路段:1500米-2000米、2000米-3000米和3000米至3500米。

所以我把我原来的reduced部分合并到一个std::list中,然后我的while(for())双循环遍历它,找出它是否有内部站点。如果有:

  1. 函数将其分为两部分(tempspeed_section1和2)
  2. 在列表中的实际redupspeed_section之前插入这些部件
  3. 删除原始redupspeed_section
  4. 将迭代器向后移动2个位置(应该是存储在temp_speed_section_1中的对象)
  5. 使用新插入的redupspeed_section继续搜索(因为原始区段中可能有更多的站点)

我的代码:

namespace split
{
/** brief  Finds reduspeed sections (left) with inner station(s) and splits them into equivalent reduspeed sections without inner stations
 *
 * param   const &reduspeeds_left  the origial vector of reduspeeds
 * param   const &stations         the stations of the line
 * return  &split_reduspeeds       the list to hold the new split and unchanged reduspeed sections
 *
 */
    bool FindOverhangingReduspeedSectionsLeft(std::vector <speed_section> const &reduspeeds_left, std::vector <station> const &stations,
                                                std::list <speed_section> &split_reduspeeds)
    {
        std::copy(reduspeeds_left.begin(), reduspeeds_left.end(),  std::back_inserter(split_reduspeeds));
        std::list<speed_section>::iterator iter_list_reduspeeds = split_reduspeeds.begin();
        int items_stations = stations.size();
        speed_section temp_speed_section_1;
        speed_section temp_speed_section_2;
        while(iter_list_reduspeeds != split_reduspeeds.end())
        {
label_1:
            for (int j=0; j<items_stations; j++)
            {
                if (iter_list_reduspeeds->its_start < stations[j].its_left_station  &&  stations[j].its_left_station < iter_list_reduspeeds->its_end)
                {
                    temp_speed_section_1.its_start = iter_list_reduspeeds->its_start;
                    temp_speed_section_1.its_end = stations[j].its_left_station;
                    temp_speed_section_1.its_speed = iter_list_reduspeeds->its_speed;
                    temp_speed_section_2.its_start = stations[j].its_left_station;
                    temp_speed_section_2.its_end = iter_list_reduspeeds->its_end;
                    temp_speed_section_2.its_speed = iter_list_reduspeeds->its_speed;
                    split_reduspeeds.insert(iter_list_reduspeeds, temp_speed_section_1);
                    split_reduspeeds.insert(iter_list_reduspeeds, temp_speed_section_2);
                    split_reduspeeds.erase(iter_list_reduspeeds);
                    /// In order to avoid the need for sorted "stations" vector/list, iterator goes to the first part of the actual reduspeed
                    --iter_list_reduspeeds;
                    --iter_list_reduspeeds;
                    goto label_1;
                }
            }
            ++iter_list_reduspeeds;
        }
        return 0;
    }

因此,该函数找到一个带有站的resduspeed部分,将其拆分为两部分,将它们插入列表,擦除原始部分并重新定位迭代器。此时,迭代器(正确地)指向speed_section对象,但该对象的成员变量具有一些随机值。while循环在下次尝试将新对象插入列表时崩溃。

我试过了,但没能找出问题出在哪里。当我将新值插入列表时,它是否可能重新分配内存,但迭代器无法"刷新"自己,或者类似的情况?

std::list::erase使给定迭代器无效,您需要将其结果存储在iter_list_reduspeeds:中

iter_list_reduspeeds = split_reduspeeds.erase(iter_list_reduspeeds);
std::advance(iter_list_reduspeeds, -2);