'c++ Is there STL algorithm to check if the range is strictly sorted?
By "strictly" I mean "without equivalent elements".
is_sorted(v.begin(), v.end(), std::less<>())
doesn't meet my goal because it returns true for ranges like 1,2,2,4,5.
is_sorted(v.begin(), v.end(), std::less_equal<>())
would work according to the implementation given here, but unfortunately is_sorted requires Compare predicate to be strict ordering (Compare(a,a) must be false), and std::less_equal certainly isn't.
So should I write my own loop for this purpose?
Solution 1:[1]
You can also make use of unique. It will return end of list if vector is unique.
bool is_strict_sorted(vector <int> a) {
return is_sorted(a.begin(), a.end()) && unique(a.begin(), a.end()) == a.end();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dushyant Singh |
