I know that the standard doesn't dictate the way that STL containers must be implemented, but rather it mandates a set of requirements for each one of them.
However, it is widely known that STL ordered containers are usually implemented as red–black trees.
You can iterate through the elements of a std::set
or a std::map
using their respective iterators, or since C++11 using ranged loops.
What puzzles me however, is how an ordered container in STL "knows" its "end". Or put it another way, since they're implemented as trees, how the end of the container is implemented or could it be implemented?
I know that the standard dictates §23.2.1/c General container requirements (Emphasis Mine):
begin() returns an iterator referring to the first element in the container. end() returns an iterator which is the past-the-end value for the container. If the container is empty, then begin() == end();
OK, for contiguous containers this is easy, but how this "past-the-end" could be materialized for trees?