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

为君一笑 提交于 2019-12-12 04:32:55

问题


I've seen some SO links wherein similiar error was seen & was suggessted to use const reference to the vector as they were copying the vector (pass by value) but in my scenario I'm using the 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

in runtime when the line

itloop !=-endIter

is hit.

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++)
   {

   }
}

回答1:


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

For example,

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

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

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.



来源:https://stackoverflow.com/questions/45204856/in-c-debug-assertion-failed-window-pops-up-i-get-vector-iterators-incompati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!