如何实现列表:<T>:迭代器?
How to implement list<T>::iterator?
我知道如何实现模板类。
但是,我不明白以下list<int>::iterator
是如何在内部实现的。我应该创建一个命名空间吗?
(基本上我想在自定义实现中使用相同的 STL 语法。
list<int> l;
list<int>::iterator it = l.begin();
简而言之,在没有任何 STL 库的情况下让代码工作的最简单方法是什么?
下面是自定义列表实现的一个片段: (为了提高可读性,我省略了大多数方法。
template <typename T>
struct list {
struct listNode {
listNode() : item(0), next(0) {}
listNode(T x) : item(x), next(0) {}
T item;
listNode* next;
};
list() : head(0),tail(0) {}
listNode* head;
listNode* tail;
};
可以在容器类型中实现迭代器类型,以便它包含有关它正在迭代的容器的任何信息。
template<class T>
class SomeContainer {
public:
class Iterator {
// You can use SomeContainer and T in here.
};
};
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- Python中的for循环与C++有何不同
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 用于AVX的ln(x)的实现,m256
- 我的 PRNG 实现与我尝试复制的实现有何不同?