如何在C ++上实现迭代器设计模式? [英] How to implement iterator design pattern on C++?
问题描述
我很好奇如何像STL在堆栈ADT中那样实现迭代器模式.
I'm curious about how to implement the iterator pattern the way STL does in a stack ADT.
#include <iostream>
#include <vector>
int main() {
std::vector<char> v = { 'a', 'b', 'c', 'd', 'e', 'f'};
std::vector<char>::iterator it = v.begin();
while(it != v.end()) {
std::cout << *it << "->";
++it;
}
std::cout << "\n";
return 0;
}
输出
a->b->c->d->e->f->
到目前为止,我已经实现了以下代码
so far I have implemented the following code
#include <iostream>
#include <memory>
template<class T>class Node {
private:
T data = 0;
std::shared_ptr<Node<T>> next_node = nullptr;
public:
Node(T data = 0, std::shared_ptr<Node<T>> next_node = nullptr)
: data(data), next_node(next_node)
{
std::cout << "created node[" << data << "]\n";
}
~Node() {
std::cout << "deleted node[" << data << "]\n";
}
// getters and setters
T getData() const {
return this->data;
}
std::shared_ptr<Node<T>> getNextNode() const {
return this->next_node;
}
void setData(T value) {
this->data = value;
}
void setNextNode(std::shared_ptr<Node<T>> node) {
this->next_node = node;
}
};
template<class T>std::ostream& operator<<(std::ostream& o, const std::shared_ptr<Node<T>> node) {
return o << "node["<< node->getData() <<"]-> ";
}
template<class T>class Stack {
private:
std::shared_ptr<Node<T>> top = nullptr;
public:
Stack()
: top(nullptr)
{ /* empty */ }
~Stack() { /* empty */ }
void push(T value) {
if(!top) {
top = std::shared_ptr<Node<T>> (new Node<T>(value));
} else {
top = std::shared_ptr<Node<T>> (new Node<T>(value, top));
}
}
void display() {
if(!top) {
std::cout << "display::The stack is empty.\n";
} else {
std::shared_ptr<Node<T>> p = top;
while(p) {
std::cout << p;
p = p->getNextNode();
}
std::cout << "\n";
}
}
class Iterator {
private:
std::shared_ptr<Node<T>> node;
public:
Iterator(std::shared_ptr<Node<T>> node)
: node(node)
{ /* empty */ }
bool hasMore() {
return node->getNextNode() != nullptr;
}
Iterator getNext() {
return Iterator(node->getNextNode());
}
int getData() {
return node->getData();
}
};
Iterator begin() const {
return Iterator(top);
}
Iterator getIterator() {
Iterator it = Iterator(top);
return it;
}
};
int main() {
Stack<char> stack;
for(char i = 'a'; i < 'f'; ++i) {
stack.push(i);
}
Stack<char>::Iterator it = stack.begin();
while(it.hasMore()) {
std::cout << it.getData() << "->";
it = it.getNext();
}
std::cout << "\n";
return 0;
}
输出:
created node[a]
created node[b]
created node[c]
created node[d]
created node[e]
101->100->99->98->
deleted node[e]
deleted node[d]
deleted node[c]
deleted node[b]
deleted node[a]
我的问题是如何为Iterator类实现嵌套模板检测,因为您可以看到预期的输出是char类型并且我正在获取整数.
My question is how to implement nested template detection for the Iterator class, as you can see the expected output is a char type and I am getting integers.
有人可以帮助我了解如何在STL中实现该功能以及如何在ADT中实现该功能吗?
Can someone help me understand how this is implemented in the STL and how it could be implemented in an ADT?
谢谢!
推荐答案
感谢注释,我能够解决此问题,我在int getData() { return node->getData(); }
上返回了错误的数据类型,我只是将int
类型更改为
Thanks for the comments I be able to fix the problem I was returning the wrong data type on int getData() { return node->getData(); }
I just change the int
type for T
type and everithing works ok!
还将bool hasMore() { return node != nullptr; }
的hasMore
方法更改为
#include <iostream>
#include <memory>
template<class T>class Node {
private:
T data = 0;
std::shared_ptr<Node<T>> next_node = nullptr;
public:
Node(T data = 0, std::shared_ptr<Node<T>> next_node = nullptr)
: data(data), next_node(next_node)
{
std::cout << "created node[" << data << "]\n";
}
~Node() {
std::cout << "deleted node[" << data << "]\n";
}
// getters and setters
T getData() const {
return this->data;
}
std::shared_ptr<Node<T>> getNextNode() const {
return this->next_node;
}
void setData(T value) {
this->data = value;
}
void setNextNode(std::shared_ptr<Node<T>> node) {
this->next_node = node;
}
};
template<class T>std::ostream& operator<<(std::ostream& o, const std::shared_ptr<Node<T>> node) {
return o << "node["<< node->getData() <<"]-> ";
}
template<class T>class Stack {
private:
std::shared_ptr<Node<T>> top = nullptr;
public:
Stack()
: top(nullptr)
{ /* empty */ }
~Stack() { /* empty */ }
void push(T value) {
if(!top) {
top = std::shared_ptr<Node<T>> (new Node<T>(value));
} else {
top = std::shared_ptr<Node<T>> (new Node<T>(value, top));
}
}
void display() {
if(!top) {
std::cout << "display::The stack is empty.\n";
} else {
std::shared_ptr<Node<T>> p = top;
while(p) {
std::cout << p;
p = p->getNextNode();
}
std::cout << "\n";
}
}
class Iterator {
private:
std::shared_ptr<Node<T>> node;
public:
Iterator(std::shared_ptr<Node<T>> node)
: node(node)
{ /* empty */ }
bool hasMore() {
return node != nullptr;
}
Iterator getNext() {
return Iterator(node->getNextNode());
}
T getData() {
return node->getData();
}
};
Iterator begin() const {
return Iterator(top);
}
Iterator getIterator() {
Iterator it = Iterator(top);
return it;
}
};
int main() {
Stack<char> stack;
for(char i = 'a'; i < 'f'; ++i) {
stack.push(i);
}
Stack<char>::Iterator it = stack.begin();
while(it.hasMore()) {
std::cout << it.getData() << "->";
it = it.getNext();
}
std::cout << "\n";
return 0;
}
输出
created node[a]
created node[b]
created node[c]
created node[d]
created node[e]
e->d->c->b->a->
deleted node[e]
deleted node[d]
deleted node[c]
deleted node[b]
deleted node[a]
这篇关于如何在C ++上实现迭代器设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!