首页 > 解决方案 > 切换向量提供的迭代器

问题描述

我正在设计自己的通用树容器,并使用 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++vectorstlcontainers

解决方案


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

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

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


推荐阅读