向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量

Vector iterators incompatible error for a vector holding iterators of another vector

本文关键字:向量 迭代器 保存 另一个 用于 错误 不兼容      更新时间:2023-10-16

参考前面的SO问题,我纠正了我的错误并将迭代器更改为相同的"向量类型",即

我更换了线路

auto startIter = table.begin((;

自动启动Iter = tabTypeIterVector[0];

在 AccessTableIteratorsVector(( 函数中的 for 循环中。 但是,在代码下方,我仍然收到"调试断言失败,矢量迭代器不兼容错误,当此行在 for 循环中被击中时

itloop !=-endIter

typedef vector<vector<string> tableDataType;
vector<tableDataType::Iterator> tabTypeIterVector;
tableDataType table;
FillRows(vector<string> vstr)
{
table.push_back(vstr);
if(some_condition_satisfied_for_this_row())
{
tableDataType::Iterator rowIT = table.end();
tabTypeIterVector.push_back(rowIT);
}
}

In another function:
AccessTableIteratorsVector()
{
auto startIter =  tabTypeIterVector[0];
auto endIter = tabTypeIterVector[1];
for(auto itloop=startIter; itloop !=-endIter;itloop++)
{
}
}

push_back可能会导致向量中包含的数据重新分配。并且这种重新分配将使向量的所有迭代器无效。取消引用无效迭代器会导致未定义的行为

对向量的索引将继续保持有效,除非您从向量中删除元素。