I have always assumed that std::lower_bound()
runs in logarithmic time if I pass a pair of red-black tree iterators (set::iterator
or map::iterator
) to it. I had to burn myself twice to notice that std::lower_bound()
runs in O(n) time in this case, at least with the libstdc++ implementation. I understand that the standard doesn't have the concept of red-black tree iterators; std::lower_bound()
will treat them as bidirectional iterators and advance
them in linear time. I still don't see any reason why the implementation couldn't create an implementation specific iterator tag for red-black tree iterators and call a specialized lower_bound()
if the passed in iterators happen to be red-black tree iterators.
Is there any technical reason why std::lower_bound()
is not specialized for red-black tree iterators?
UPDATE: Yes, I am aware of the find member functions but it is so not the point. (In a templated code I may not have access to the container or work only on a part of container.)
After the bounty has expired: I find Mehrdad's and Yakk's answers the most convincing. I couldn't decide between the too; I am giving the bounty to Mehrdad and accepting Yakk's answer.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…