在C ++中,弹出Debug assertion failed窗口&我得到矢量迭代器不兼容的错误运行时 [英] In C++, Debug assertion failed window pops up & I get vector iterators incompatible error runtime

查看:268
本文介绍了在C ++中,弹出Debug assertion failed窗口&我得到矢量迭代器不兼容的错误运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些SO链接,其中发现了类似的错误。是因为他们正在复制teh向量(通过值传递)而使用const引用使用const引用,但在我的场景中我使用相同的向量(没有通过值)。看到这个问题。 WRT低于代码,我看到错误

I've seen some SO links wherein similiar error was seen & was suggessted to use const reference to teh vector as they were copying teh vector (pass by value) but in my scenario I'm using teh same vector (no pass by value). BUt seeing this issue. WRT below code, I'm seeing the error


调试断言失败窗口弹出&我得到矢量迭代器
不兼容错误

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 对象。

It looks like you are comparing two iterators that correspond to different vector objects.

例如,

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

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

即使 iter_a iter_b 属于同一类型,不允许比较它们。使用 iter_a == iter_b iter_a!= iter_b 是未定义行为的原因。

Even though iter_a and iter_b are of the same type, comparing them is not allowed. Use of either iter_a == iter_b or iter_a != iter_b is cause for undefined behavior.

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

It's not clear from your post why you have the need for comparing the iterators but you'll have to rethink your implementation strategy.

这篇关于在C ++中,弹出Debug assertion failed窗口&amp;我得到矢量迭代器不兼容的错误运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆