In this question I’m not asking how to do it but HOW IS IT DONE.
I’m trying (as an excersise) implement simple map and although I do not have problems with implementing links and they behavior (how to find next place to insert new link etc.) I’m stuck with the problem how to implement iterating over a map. When you think about it and look at std::map this map is able to return begin and end iterator. How? Especially end?
If map is a tree how can you say which branch of this map is an end? I just do not understand it. An how to iterate over a map? Starting from the top of the tree and then what? Go and list everything on the left? But those nodes on the left have also links to the right. I really don’t know. I will be really glad if someone could explain it to me or give me a link so I could read about it.
In this question I’m not asking how to do it but HOW IS IT
Share
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.:Or something similar. Now,
mymap::begin()could return such instance of the iterator thatnwould point to the leftmost node.mymap::end()could return instance withnpointing 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.The operation of moving between the nodes (
operators++()andoperator--(), 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.