Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) IsSorted

 

IsSorted is a check code snippet to check if a std::vector is sorted.

 

There are multiple versions of IsSorted:

 

 

 

 

 

 

C++98 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();
}

 

 

 

 

 

C++98 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();
}

 

 

 

 

 

C++98 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();
}

 

 

 

 

 

C++11 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());
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict