【问题标题】:Vector iterators incompatible error for a vector holding iterators of another vector包含另一个向量的迭代器的向量的向量迭代器不兼容错误
【发布时间】:2017-12-29 02:31:29
【问题描述】:

参考这个previous SO question,我纠正了我的错误并将迭代器更改为相同的“向量类型”,即

我换了行

auto startIter = table.begin();

auto startIter = tabTypeIterVector[0];

在 AccessTableIteratorsVector() 函数的 for 循环中。 但是,在下面的代码中,我仍然收到“调试断言失败,向量迭代器不兼容错误, 当此行在 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++)
   {

   }
}

【问题讨论】:

  • 为什么你要存储一个迭代器向量?应该解决的实际问题是什么?
  • ……你为什么不简单地存储索引,它不会变得无效。

标签: c++ c++11 vector iterator incompatibletypeerror


【解决方案1】:

push_back 可能会导致向量中包含的数据重新分配。而这种重新分配将使向量的所有迭代器无效。取消引用无效的迭代器会导致 undefined behavior

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

【讨论】:

  • 是否可以从有效的向量迭代器中获取索引?
  • 我会更担心end() 向量是被存储的向量。
猜你喜欢
  • 2012-01-15
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多