Monday, September 11, 2017

C++11: Algorithms: is_sorted

C++11 added an is_sorted algorithm. Here is an example:

#include <algorithm>
#include <iostream>

int main()
{
  int myArray[10]  = {1, 2, 3, 8, 9, 10, 10, 10, 10, 10};

  int * inIterBegin   = &myArray[0];
  int * inIterEnd     = &myArray[5];

  bool arrayIsSorted = std::is_sorted(inIterBegin , inIterEnd);
  
  std::cout << arrayIsSorted << " ";

  myArray[4] = 0;
  arrayIsSorted = std::is_sorted(inIterBegin , inIterEnd);

  std::cout << arrayIsSorted << std::endl;
  return 0;
}
// Output: 1 0
Reference: https://isocpp.org/wiki/faq/cpp11-library-stl#cpp11-algorithms

No comments:

Post a Comment