当列表重新分配内存时,我应该刷新list::迭代器吗
Should I refresh list::iterator when the list reallocates memory?
想象一条铁路线。我们有一些车站和路段,列车在那里减速行驶。这些区段可以包含工作站。我需要将这些部分拆分为不包含工作站的部分。
例如:从线路的1500米到3500米,列车只能以40公里/小时的速度行驶。我在2000米和3000米有两个车站。在这种情况下,我需要有三个路段:1500米-2000米、2000米-3000米和3000米至3500米。
所以我把我原来的reduced部分合并到一个std::list中,然后我的while(for())双循环遍历它,找出它是否有内部站点。如果有:
- 函数将其分为两部分(tempspeed_section1和2)
- 在列表中的实际redupspeed_section之前插入这些部件
- 删除原始redupspeed_section
- 将迭代器向后移动2个位置(应该是存储在temp_speed_section_1中的对象)
- 使用新插入的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);
相关文章:
- 我应该使用什么来代替void作为变体中的替代类型之一
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 我应该删除矢量<short>吗?
- 我应该如何修改此代码以使用给定字符串中的字母打印菱形图案
- 我应该在锁定TBitmap画布后解锁它吗
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 我应该避免多重实现继承吗
- 为了方便起见,我应该避免公开私有字段变量吗
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 违反const正确性:我应该现实地期待什么问题
- 我应该如何表示我拥有的连续元素序列?
- 我应该将除 .cpp 以外的其他文件添加到 git 中吗?
- 我应该如何从 stdin C++ 中读取可变长度的格式字符串?
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 我应该如何捕捉out_of_range异常?
- 我应该声明所有不抛出 noexexcept 的成员/函数吗?
- 我应该从 GetDisplayModeList 中使用哪种刷新率
- 当列表重新分配内存时,我应该刷新list::迭代器吗