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() and back() 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 with push_front() and pop_front(). For immutable sequences (of which the newcomer array is the only example in the standard, unless you count const 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 them front() either. You can add entries to map, 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 has front() but not push_front()". I suspect that this is because vector has back() -- if you're using back() then again it's "nice" to use front() 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.