模板迭代器,没有STL
Template iterator, no STL
我不确定这在这里适合多少,我对c++有点陌生,但我必须用迭代器在动态数组/链表上创建ADT。我的问题是,有没有办法让这个迭代器成为模板这样它就可以同时适用于动态数组和链表或者我必须为一个类迭代器做两种不同的实现?
我在想这样的事情:
template<typename Container>
class Iterator {
private:
Container *cont;
int pos;
public:
Iterator();
Iterator(const Container& c);
~Iterator();
bool isValid();
void operator++();
void operator--();
Element getCurrent();
};
显然,它不知道Element是什么,这就是我的问题。是否有任何方法让getCurrent()从当前位置返回元素?这有什么结果吗?
在c++ 14中可以这样做:
template<typename Container>
class Iterator {
private:
Container *cont;
int pos;
public:
Iterator();
Iterator(const Container& c);
~Iterator();
bool isValid();
void operator++();
void operator--();
auto getCurrent(); // the return type is automatically generated
};
,但在c++11中你必须删除返回类型
所以getCurrent ();必须返回一个void *并使用reinterpret_cast(getCurrent());
实现修正:(what I think)
template<typename Container>
class Iterator {
private:
Container *cont;
int currentPos;
int beginPos;
int endPos;
public:
Iterator();
Iterator(const Container& c,int endpos)currentPos{0},beginPos{0},endPos{endpos};
~Iterator();
void operator++();
void operator--();
auto& getCurrent()const;
}
相关文章:
- 有没有办法捕获 STL 函数未被赋予正确参数的异常?
- 没有 STL 容器的迭代器
- 有没有一种 STL 方法可以找到字符串的所有排列,给出一个以 C++ 为单位的大小?
- C++-STL-矢量-为什么矢量中没有指示relocation_count的工具
- STL:在没有输出的情况下处理两个集合
- 有没有办法从 STL 获取无开销的原始内存容器
- 当遇到不是来自 STL 的未捕获异常时,有没有办法打印其信息?
- 如何删除在没有STL的队列中存储在数组中的第一个字符串元素
- 有没有一种 stl 方法来执行指针向量的深度复制
- C 从没有STL的用户输入中获得CSVWITH的最大值
- 没有碎片stl容器的大容量内存
- 没有stl和动态内存分配的成员函数类
- 是否可以在没有push_back(C++STL)的情况下使用数组初始化矢量的新位置
- 有没有STL函数可以得到两个C++向量的笛卡尔乘积
- C++在没有STL的情况下调整指针数组的大小(向量..)
- 没有STL的C++动态数组
- 如何在没有STL的情况下从2d阵列中删除任何未填充的插槽
- 如何在没有STL的情况下用c++中的Key创建和访问内存块
- 如何列出所有的变化没有STL c++
- 模板迭代器,没有STL