向量迭代器不相容误差,用于保持另一向量的迭代

向量迭代器不相容误差,用于保持另一向量的迭代

问题描述:

参照该previous SO question的载体,我纠正我的错误&改变了迭代器是相同的“载体类型”即向量迭代器不相容误差,用于保持另一向量的迭代

我取代了线

auto startIter = table.begin();

自动startIter = tabTypeIterVector [0];

in for循环中的AccessTableIteratorsVector()函数。 WRT下面的代码,但是,我仍然得到“调试断言失败,矢量迭代器不兼容的错误, 当这条线被击中在for循环

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

    } 
} 
+1

*为什么*你想存储一个迭代器向量?应该解决的*实际*问题是什么? –

+1

...为什么你不是简单地存储索引,它不会失效。 – spectras

push_back可能会导致向量中包含的数据重新分配,并且该重新分配将使所有迭代器的向量无效解除引用无效的迭代器会导致undefined behavior

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

+0

是否有可能从有效的向量迭代器中获取索引? – codeLover

+1

我会更关注'end()'向量是存储的。 – juanchopanza