且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在地图上迭代

更新时间:2022-12-20 16:48:12

地图的迭代器的表示完全取决于您。我认为应该足够使用一个单一的包指针到节点。例如:

The representation of your map's iterator is totally up to you. I think it should suffice to use a single wrapped pointer to a node. E.g.:

template <typename T>
struct mymapiterator
{
  typename mymap<T>::node * n;
};

或类似的东西。现在, mymap :: begin()可以返回这样的迭代器实例, n 将指向最左边的节点。 mymap :: end()可以返回 n 指向根或者其他特殊节点的实例仍然可以回到最右边的节点,使得它可以满足来自结束迭代器的双向迭代。

Or something similar. Now, mymap::begin() could return such instance of the iterator that n would point to the leftmost node. mymap::end() could return instance with n pointing to root probably or some other special node from which it is still possible to get back to rightmost node so that it could satisfy bidirectional iteration from end iterator.

在节点之间移动的操作( operator ++ ) operator - ()等)是关​​于遍历树从较小值到较大值,反之亦然。您可能已经在插入操作实现期间实现的操作。

The operation of moving between the nodes (operators++() and operator--(), etc.) are about traversing the tree from smaller to bigger values or vice versa. Operation that you probably have already implemented during insertion operation implementation.