在 C++ 中,弹出 Debug assertion failed window & 我得到 vector iterators incompatible error runtime

In C++, Debug assertion failed window pops up & I get vector iterators incompatible error runtime

我看到了一些 SO 链接,其中看到了类似的错误,并建议在复制向量(按值传递)时使用对向量的 const 引用,但在我的场景中,我使用的是相同的向量(没有价值传递)。但是看到这个问题。 WRT 下面的代码,我看到错误

Debug assertion failed window pops up & I get vector iterators incompatible error

在运行时当行

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 = table.begin();
auto endIter = tabTypeIterVector[0];
   for(auto itloop=startIter; itloop !=-endIter;itloop++)
   {

   }
}

看起来您正在比较对应于不同 vector 对象的两个迭代器。

例如,

std::vector<int> a(5);
std::vector<int> b(5);

auto iter_a = a.begin();
auto iter_b = b.begin();

即使iter_aiter_b是同一类型,也不允许进行比较。使用 iter_a == iter_biter_a != iter_b 会导致未定义的行为。

从您的 post 中不清楚为什么您需要比较迭代器,但您必须重新考虑您的实施策略。