The thing which puzzles you most may be this statement in the link you refered to:
I have created a hash map
However, this is incorrect. std::map
is a tree map which is typically implemented by red-black tree.
Now your question basically turns to a data structure problem.
red-black tree is a kind of binary search tree, every node in which has the following properties:
- the left son of the node is less than the current node
- the right son of the node is greater than the current node
Note that I used "less" or "greater" only to let you understand. More pricisely, given a binary predicate p
used to organize the tree which satisfys the requirements of strict weak ordering, we assume the current node is N, then:
p(N, N->left)
is true
p(N, N->right)
is false
So now you are at the top of the tree, to find the correct node, you need to keep comparing the value you have with the current node and decide which direction you should go.
And you may ask why ==
is not necessary, that's because when a predicate p satisfys the requirement of strict weak ordering, the following two expressions are equivalent(Not precise, but at lease the part we concerned about is equivalent):
A. !(p(A, B) || p(B, A))
B. A == B
you can ask yourself, when A is not less than B and B is not less than A, what's the relationship between A and B?