I cannot find any official definition related to handling of std::nth_element(first, nth, last) if nth is outside range [first, last).
As an attempt to figure it out, I did a toy test on my machine:
std::vector<int> arr{ 7, 3, 9, 6, 4 };
std::nth_element( arr.begin() + 1, arr.begin(), arr.end() );
for (int num : arr) {
printf("%d", num);
}
MSVC gives me assertion fail in stl src code of vector, saying "vector iterator range transposed". Is it an undefined behavior?
I cannot find any official definition related to handling of std::nth_element(first, nth, last) if nth is outside range [first, last).
As an attempt to figure it out, I did a toy test on my machine:
std::vector<int> arr{ 7, 3, 9, 6, 4 };
std::nth_element( arr.begin() + 1, arr.begin(), arr.end() );
for (int num : arr) {
printf("%d", num);
}
MSVC gives me assertion fail in stl src code of vector, saying "vector iterator range transposed". Is it an undefined behavior?
Share Improve this question asked Nov 19, 2024 at 12:11 PkDrewPkDrew 1,0702 gold badges7 silver badges23 bronze badges 1 |1 Answer
Reset to default 4Yes, it is undefined behaviour if
[first, nth) or [nth, last) is not a valid range.
Which says as much as that nth
needs to lie between first
and last
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745562792a4633207.html
std::nth_element
, and it covers your case. – Jarod42 Commented Nov 19, 2024 at 13:35