c++setapi-designmember-functionsdesign-decisions

Is there a design reason why std::set doesnt have front and back member functions?


  1. I know I can use *s.begin(), but same argument can be used for vector, which has front/back
  2. often I use the ordered property of set/map to get "smallest" element/key - ofc the fact that I do it is not the reason to have it, just a example :)

Here I'm talking about design reasons why front/back would be bad design, so please skip obvious reasons like committee forgot about it...


Solution

  • I imagine that the words "front" and "back" are reserved for sequence containers (i.e. those where the order of elements is determined by the order of insertion), and the words are meant to suggest a physical position in that sequence.

    Since set is not a sequence container (but instead an associative container), this isn't appropriate. In particular, note that the meaning of "front" can change by later insertions of an unrelated element.