向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量 [英] Vector iterators incompatible error for a vector holding iterators of another vector

查看:341
本文介绍了向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考此以前的问题,我纠正了我的错误&将迭代器更改为相同的矢量类型即

With reference to this previous SO question , I corrected my mistake & changed the iterators to be of same "vector type" i.e.

我替换了行


auto startIter = table.begin();

auto startIter = table.begin();

with


auto startIter = tabTypeIterVector [0];

auto startIter = tabTypeIterVector[0];

在AccessTableIteratorsVector()函数的for循环中。
wrt低于代码,但是,我仍然得到调试断言失败,向量迭代器不兼容错误,
当此行被命中for for循环

in the for loop in AccessTableIteratorsVector() function. wrt below code, however, I'm still getting the "Debug assertion failed, vector iterators incompatible error, when this line is hit in for loop


itloop!= - endIter

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

   }
}


推荐答案

push_back 可能会导致重新分配向量中包含的数据。并且该重新分配将使向量无效的所有迭代器。取消引用无效的迭代器会导致 未定义的行为

push_back might cause a reallocation of the data contained in the vector. And that reallocation will make all iterators to the vector invalid. Dereferencing invalid iterators leads to undefined behavior.

向量中的索引将继续保持有效,除非您从向量中删除元素。

Indexes into the vector will continue to stay valid, unless you remove elements from the vector.

这篇关于向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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