我想在这里弄清楚几件事:
如何为下面的类实现迭代器?
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Node {
public:
Node(int i=0):val(i) {}
Node*& operator++(int i=0) {return next;};
T val;
Node *next;
};
//================================================
int main() {
Node<int> *head, *tmp1, *tmp2;
tmp1 = new Node<int>(0);
head = tmp1;
for (int i=1; i<10; ++i) {
tmp2 = new Node<int>(i);
tmp1->next = tmp2;
tmp1 = tmp2;
}
while (head != NULL) {
cout << head->val << " '";
head = head->operator++(0); //How do I make it work with ++head;?
}
}
这不是演示运算符重载或迭代器的好例子。
答案 0 :(得分:11)