C++ N 叉树,使用接受参数的后缀运算符构建自定义迭代器
我正在尝试构建一个 N 叉树自定义迭代器。树中的节点 (MoveTreeNode*
) 具有用于转到前一个节点的成员值 MoveTreeNode* m_parent
,以及 vector
转到下一个节点。
我希望迭代器的后缀运算符采用参数值 int i
,该值表示迭代节点应在其 m_children
向量中下降的索引。
节点的键是 Move
类型的对象。
MoveTreeNode.hpp
#pragma once
#include "Move.hpp"
using namespace std;
class MoveTreeNode {
public:
MoveTreeNode(Move&);
Move& m_move; // Key
MoveTreeNode* m_parent; // To go to previous move
vector<MoveTreeNode*> m_children; // To to go to next move
};
MoveTree.hpp
#pragma once
#include "Move.hpp"
#include "MoveTreeNode.hpp"
using namespace std;
class MoveTree {
public:
MoveTreeNode* m_root = nullptr; // Root of the tree
// ( ... some member functions here)
struct Iterator {
using iterator_category = bidirectional_iterator_tag;
using difference_type = ptrdiff_t;
Iterator(MoveTreeNode* ptr): m_node(ptr) {};
MoveTreeNode& operator*() const { return *m_node; }
MoveTreeNode* operator->() { return m_node; }
// Prefix increment
// Iterator& operator++(int i) {
// m_node= m_node->m_children.at(i); return *this;
// }
// ^^^^^^error : cannot overload functions distinguished by return type alone
// Postfix increment
Iterator operator++(int i) {
Iterator tmp = m_node; m_node= m_node->m_children.at(i); return tmp;
}
// Prefix decrement
Iterator& operator--() { m_node = m_node->m_parent; return *this; }
// Postfix decrement
Iterator operator--(int) {
Iterator tmp = m_node; m_node = m_node->m_parent; return tmp;
}
friend bool operator== (const Iterator& a, const Iterator& b) { return a.m_node== b.m_node; };
friend bool operator!= (const Iterator& a, const Iterator& b) { return a.m_node!= b.m_node; };
private:
MoveTreeNode* m_node;
};
Iterator begin() { return Iterator(m_root); }
Iterator end() { return Iterator(nullptr); }
};
遇到的问题
- 是否可以重载增量运算符,使其接受参数,在本例中是表示索引的整数?
- 我不确定是否必须同时指定前缀和后缀运算符?如果我从不使用前缀运算符,我可以只定义后缀运算符吗?因为在上面的代码片段中,如果我取消注释前缀定义,则会收到编译错误无法重载仅按返回类型区分的函数,并且我不知道如何对此做出响应。
- 这是我第一次尝试构建自定义迭代器,我的上述结构是否正确,或者是否有我遗漏的内容并且应该修改?
I am trying to build a N-ary tree custom iterator. The nodes in the tree (MoveTreeNode*
) have member values MoveTreeNode* m_parent
to go to the previous node, and vector <MoveTreeNode*> m_children
to go to the next nodes.
I want my iterator's postfix operator to take the parameter value int i
which represents the index where the iterated node should descend in its m_children
vector.
The key of the node is an object of type Move
.
MoveTreeNode.hpp
#pragma once
#include "Move.hpp"
using namespace std;
class MoveTreeNode {
public:
MoveTreeNode(Move&);
Move& m_move; // Key
MoveTreeNode* m_parent; // To go to previous move
vector<MoveTreeNode*> m_children; // To to go to next move
};
MoveTree.hpp
#pragma once
#include "Move.hpp"
#include "MoveTreeNode.hpp"
using namespace std;
class MoveTree {
public:
MoveTreeNode* m_root = nullptr; // Root of the tree
// ( ... some member functions here)
struct Iterator {
using iterator_category = bidirectional_iterator_tag;
using difference_type = ptrdiff_t;
Iterator(MoveTreeNode* ptr): m_node(ptr) {};
MoveTreeNode& operator*() const { return *m_node; }
MoveTreeNode* operator->() { return m_node; }
// Prefix increment
// Iterator& operator++(int i) {
// m_node= m_node->m_children.at(i); return *this;
// }
// ^^^^^^error : cannot overload functions distinguished by return type alone
// Postfix increment
Iterator operator++(int i) {
Iterator tmp = m_node; m_node= m_node->m_children.at(i); return tmp;
}
// Prefix decrement
Iterator& operator--() { m_node = m_node->m_parent; return *this; }
// Postfix decrement
Iterator operator--(int) {
Iterator tmp = m_node; m_node = m_node->m_parent; return tmp;
}
friend bool operator== (const Iterator& a, const Iterator& b) { return a.m_node== b.m_node; };
friend bool operator!= (const Iterator& a, const Iterator& b) { return a.m_node!= b.m_node; };
private:
MoveTreeNode* m_node;
};
Iterator begin() { return Iterator(m_root); }
Iterator end() { return Iterator(nullptr); }
};
Issues encountered
- Is it possible to have the increment operator overloaded such that it takes in a parameter, in this case an integer representing an index ?
- I am not sure whether both prefix and postfix operators have to be specified? If I never use the prefix operator can I just define the postfix operator ? Because in the above snippet of code if I uncomment the prefix definition then I get the compilation error cannot overload functions distinguished by return type alone and I am not sure how to respond to that.
- It's my first time trying to build a custom iterator, is my above struct correct, or is there something I am missing and should modify ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论