Can partial_sort's middle iterator equal the end iterator?

Viewed 91

According to http://www.cplusplus.com/reference/algorithm/partial_sort/, the middle argument is a:

Random-access iterator pointing to the element within the range [first,last) that is used as the upper boundary of the elements that are fully sorted.

Specifically, middle isn't allowed to be equal to last. However, https://en.cppreference.com/w/cpp/algorithm/partial_sort seems to have entirely different documentation and doesn't mention any range restrictions (but obviously middle shouldn't be outside [first, last]).

Is behavior defined when middle == last?

2 Answers

Can partial_sort's middle iterator equal the end iterator?

Yes, it can. In that case the effect is same as using std::sort.

The standard specifies the preconditions like this:

Preconditions: [first, middle) and [middle, last) are valid ranges.

The description of std::partial_sort in the standard (N4659, 28.7.1.3/2) reads

  1. Effects: Places the first middle - first sorted elements from the range [first, last) into the range [first, middle). The rest of the elements in the range [middle, last) are placed in an unspecified order.

I see nothing here that would prohibit middle from equaling last.

Related