Generic iterator(泛型迭代器)
问题描述
我正在尝试找到一种访问一组容器的通用方法.除了另一个自定义列表之外,我还有一个标准向量和列表.
I am trying to find a generic way of accessing a set of containers. I have a standard vector and list in addition to another custom list.
自定义列表定义了一个迭代器;
The custom list defines an iterator;
class Iterator: public std::iterator<std::forward_iterator_tag, T> {
// ...
}
Iterator begin() {
return (Iterator(root));
}
Iterator end() {
return (Iterator(NULL));
}
适当的运算符重载.
理想情况下,我愿意这样做;
Ideally, I would like to do this;
class Foo {
public:
Foo() {
std::list<int> x;
std::vector<int> y;
custom_list<int> z;
iter = x.begin(); // OR
iter = y.begin(); // OR
iter = z.begin();
// ...
};
private:
std::iterator<int> iter;
};
但显然这些都是不同类型的迭代器.不过,我可以假设所有容器都是同一类型.
But obviously these are all iterators of different types. I can assume all the containers are of the same type however.
有没有优雅的方法来解决这个问题?
Is there an elegant way to solve this problem?
推荐答案
这里有一些你可能会感兴趣的文章
Here are some articles you might find of interest
给 STL 迭代器一个基类
C++ 迭代器的类型擦除
any_iterator 类参考
这篇关于泛型迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:泛型迭代器


基础教程推荐
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01