Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 401
NoSenseEtAl Avatar asked May 21 '13 08:05

NoSenseEtAl


People also ask

Is std :: set ordered C++?

Per the C++ standard, iteration over the elements in an std::set proceeds in sorted order as determined by std::less or by the optional comparison predicate template argument.

Are STD sets sorted?

std::set is an associative container that contains a sorted set of unique objects of type Key . Sorting is done using the key comparison function Compare. Search, removal, and insertion operations have logarithmic complexity.

Does set preserve order C++?

No, it does not.


1 Answers

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.

like image 196
Kerrek SB Avatar answered Oct 02 '22 11:10

Kerrek SB