Why no front() method on std::map (and other associative containers from the STL)?
You really have to ask the standards committee on that one (comp.lang.c++.std) but my guess is that yeah, it just doesn't make as much sense. Further there's not as much clarity as to what it would mean. Do you want the root, the pre-order first, post-order first, first you inserted...? With sequences it's quite clear: front is one side, back the other. Maps are trees.
Front() implies an ordering; "the first in the row".
Begin() implies lets start somewhere, no matter where.
I speculate that:
front()
andback()
would not exist in Sequence if not for the fact that the interface was originally designed with mutable sequences in mind.front()
makes most sense when you think about how you'd use it in combination withpush_front()
andpop_front()
. For immutable sequences (of which the newcomerarray
is the only example in the standard, unless you countconst vector
),front()
is a shorthand for*begin()
that simply is not worth getting excited about.Since non-sequence ordered containers don't have
push_front()
, it was not thought worth giving themfront()
either. You can add entries tomap
, but you can't specify where in the order to add them since that's that the key is for. This is the difference between a sequence vs. an ordered collection."Hang on", you say, "
vector
hasfront()
but notpush_front()
". I suspect that this is becausevector
hasback()
-- if you're usingback()
then again it's "nice" to usefront()
to match it.
This is just speculation, though, based on what I know about designing useful/satisfying APIs, and my observation of the container APIs. I have no knowledge of Stepanov's thinking on the matter, or of any record of its discussion in the standard committee.