c++ - 切换向量提供的迭代器
问题描述
我正在设计自己的通用树容器,并使用 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 矢量接口中的缺陷,但看起来确实如此。
所以我的问题是:在设计我自己的容器时有没有办法避免这种情况?
解决方案
传递给容器成员函数的迭代器必须引用该容器内的元素(或者,在某些情况下,返回的结束元素end()
)。如果迭代器没有引用容器,则您有未定义的行为。
没有简单的方法可以避免这种情况。最接近的方法是验证迭代器,这意味着您必须跟踪每个迭代器所属的容器。swap
对于某些操作,例如不会insert
使现有迭代器无效但让它们引用新容器的操作,这会变得有点复杂。
一些编译器,例如在调试模式下编译的 Visual C++,可以在运行时检测到这些类型的问题并发出适当的通知。
推荐阅读
- python - 使用 pandas 操作数据时出错(以防数据丢失)
- java - 在 google colab 中上传文件
- c++ - 无法生成预期的输出
- splunk - 在 splunk 中将国旗表情符号注释到 3166 个国家/地区代码
- javascript - 导入 JSPDF:未捕获的 SyntaxError:无法在模块外使用 import 语句
- sql - SQL - 如何计算期初余额和期末余额
- c - strtok 返回 null 但不返回 printf
- python - 在 plotly 中绘制带有几列的数据框
- docker - 带有电子邮件的 Keycloak 初始管理员用户
- python - 避免不必要的类声明