(C++) IsSorted
January 7, 2018 · View on GitHub
(C++) IsSorted
IsSorted is a check code snippet to check if a std::vector is sorted.
There are multiple versions of IsSorted:
IsSorted for
std::vector using the C++98
STL
IsSorted for any
container using the C++98
STL
IsSorted for any
container using the C++11
STL
IsSorted for std::vector using the C++98 STL
#include <algorithm> #include <functional> #include <vector> ///IsSorted checks if a std::vector is sorted. ///From http://www.richelbilderbeek.nl/CppIsSorted.htm template <class T> bool IsSorted(const std::vector<T>& v) { return std::adjacent_find( v.begin(), v.end(), std::greater<T>()) == v.end(); }
IsSorted for a std::vector using the C++98 STL
#include <algorithm> #include <functional> #include <vector> ///IsSorted checks if a std::vector is sorted. ///From http://www.richelbilderbeek.nl/CppIsSorted.htm template <class T> bool IsSorted(const std::vector<T>& v) { return std::adjacent_find( v.begin(), v.end(), std::greater<T>()) == v.end(); }
IsSorted for any container using the C++98 STL
#include <algorithm> #include <functional> #include <vector> ///IsSorted checks if a container is sorted. ///From http://www.richelbilderbeek.nl/CppIsSorted.htm template <class T> bool IsSortedStl98(const T& v) { return std::adjacent_find( v.begin(), v.end(), std::greater<typename T::value_type>()) == v.end(); }
IsSorted for any container using the C++11 STL
#include <algorithm> #include <functional> #include <vector> ///IsSorted checks if a container is sorted. ///From http://www.richelbilderbeek.nl/CppIsSorted.htm template <class T> bool IsSortedStl11(const T& v) { return std::is_sorted(v.begin(),v.end()); }