As the elements of the std::set are in sorted order and each element is unique. Also we can't modify the elements of the set, then why the library provides the begin() and end() functions as we already have cbegin() and cend().
Because if you removed these functions, you would not be able to use std::set in many places where a Container is expected. The most obvious one (to me)1:
for (auto x: mySet) { }
Would not be possible because the range-based for loop rely on begin and end, not cbegin or cend (§6.5.4/1.3).
One other example where this would pose a problem would be the std::begin2 function, which, as you can see in the link, rely on c.begin() and not c.end().
The basic idea is that you do not want std::set to have a different interface than the other standard containers.
1 There are many places like that, this is just one example among others.
2 Note that even std::cbegin rely on the const-overload of c.begin() rather than on c.cbegin().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With