【发布时间】: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