简体   繁体   中英

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

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

   }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM