在C++中,调试断言失败窗口弹出&我得到矢量迭代器不兼容错误运行时

问题描述:

我见过一些SO链接,其中看到类似的错误& suggessted使用常量引用的矢量,因为他们正在复制矢量按价值),但在我的情况下,我正在使用相同的向量(没有按值传递)。看到这个问题。下面的代码WRT,我看到的错误在C++中,调试断言失败窗口弹出&我得到矢量迭代器不兼容错误运行时

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

在运行时行

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会导致未定义的行为。

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

+0

谢谢。我纠正了我的错误,但仍然观察到同样的错误。在这里提出了一个不同的问题https://*.com/questions/45264761/vector-iterators-incompatible-error-for-a-vector-holding-iterators-of-another-ve – codeLover