【问题标题】:Switching Vectors Supplied Iterators切换向量提供的迭代器
【发布时间】:2020-09-13 23:41:00
【问题描述】:

我正在设计自己的通用树容器,并使用 STL 作为参考。然而,在实现我的迭代器类时,我注意到 STL 对迭代器的使用。

例如,std::vector 类依赖迭代器作为其许多方法的参数。 (即erase(const_iterator position)

这让我想知道:如果给定两个相同模板类型的向量,并且在方法调用中将第一个向量迭代器提供给第二个向量,会发生什么情况?为了帮助回答这个问题,我编写了一个简单的程序来说明我的想法。

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

void printVec(const std::string &label, const std::vector<int> &vec){
    for (unsigned int i=0; i<vec.size(); i++){
        std::cout << ::std::setw(3) << vec[i] << ", ";
    }
    std::cout << std::endl;
}

int main()
{
  std::vector<int> test={0,1,2,3,4,5,6,7,8,9};
  std::vector<int> test2{10,11,12,13,14,15,16,17,18,19};
  std::vector<int>::iterator iter=test.begin();
  std::vector<int>::iterator iter2=test2.begin();

  printVec("One",test);
  printVec("Two",test2);

  for (int i=0; i<5; i++, iter++, iter2++);
  std::cout << "One Pos: " << *iter << std::endl;
  std::cout << "Two Pos: " << *iter2 << std::endl;
  test.erase(iter2);    //Switching the iterators and there respective vectors
  test2.erase(iter);    //Switching the iterators and there respective vectors

  printVec("One",test);
  printVec("Two",test2);
}

运行这个程序会导致一个段。错误,这似乎表明这是未定义的行为。我不敢将其称为 STL 矢量接口中的缺陷,但看起来确实如此。

所以我的问题是:在设计我自己的容器时有什么方法可以避免这种情况?

【问题讨论】:

    标签: c++ vector stl containers


    【解决方案1】:

    传递给容器成员函数的迭代器必须引用该容器中的一个元素(或者,在某些情况下,是end() 返回的结束元素)。如果迭代器没有引用你有未定义行为的容器。

    没有简单的方法可以避免这种情况。最接近的方法是验证迭代器,这意味着您必须跟踪每个迭代器所属的容器。对于一些操作,例如 swapinsert,这会变得有点复杂,它们不会使现有迭代器无效,而是让它们引用新容器。

    一些编译器,例如在调试模式下编译的 Visual C++,可以在运行时检测到这类问题并发出适当的通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 2020-11-06
      • 2017-08-29
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多