Too bad it isn' t in the C++ std.
It does exist in stl. One can use "adjacent_find" with predicate "less" to obtain the same result
Container container; // e.g. typedef vector<int> Container; ... if (adjacent_find(container.begin(),container.end(),greater<Container::value_type>())==container.end()) ...; // container is sorted
Of course, greater (i.e. operator>() ) may not be defined for the type in question. In any case, here is the is_sorted duo:
template <class ForwardIterator> bool is_sorted(ForwardIterator first, ForwardIterator last) { if (first!=last) for (ForwardIterator it(first++);first!=last;++it /* or it=first */,++first) if (*first<*it) return false; return true; } template <class ForwardIterator, class Predicate> bool is_sorted(ForwardIterator first, ForwardIterator last, Predicate pred) { if (first!=last) for (ForwardIterator it(first++);first!=last;++it /* or it=first */,++first) if (!pred(*it,*first)) return false; return true; }
If anyone finds bugs or can improve performance, please do so...
Container container; // e.g. typedef vector<Foo> Container; ... if (adjacent_find(container.rbegin(),container.rend(),less<Container::value_type>())==container.rend()) ...; // container is sortedPeople/Andre Mostert