Source of is_sorted_until2a.cpp


  1: //is_sorted_until2a.cpp

  3: #include <iostream>
  4: #include <iomanip>
  5: #include <vector>
  6: #include <algorithm>
  7: #include <functional>
  8: using namespace std;

 10: int main()
 11: {
 12:     cout << "\nThis program illustrates the use of the STL "
 13:         "is_sorted_until() algorithm from\n<algorithm> to "
 14:         "find the first integer that destroys the sorting "
 15:         "order of a\nvector of integers into descending order.";
 16:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 18:     int a[] = {5, 4, 3, 2, 1};
 19:     int b[] = {5, 4, 3, 0, 1};
 20:     vector<int> va(a, a+5);
 21:     vector<int> vb(b, b+5);
 22:     cout << boolalpha;
 23:     auto firstUnsortedElementIter =
 24:         is_sorted_until(va.begin(), va.end(), greater<int>());
 25:     if (firstUnsortedElementIter == va.end()) 
 26:         cout << "For 5 4 3 2 1, the sequence is sorted "
 27:             "in descending order." << endl;
 28:     else
 29:         cout << "For 5 4 3 2 1, the sequence sort was "
 30:         "destroyed at element " << *firstUnsortedElementIter << ".\n";

 32:     firstUnsortedElementIter =
 33:         is_sorted_until(vb.begin(), vb.end(), greater<int>());
 34:     if (firstUnsortedElementIter == vb.end()) 
 35:         cout << "For 5 4 3 0 1, the sequence is sorted "
 36:             "(in descending order)." << endl;
 37:     else
 38:         cout << "For 5 4 3 0 1, the sequence sort was "
 39:         "destroyed at element " << *firstUnsortedElementIter << ".\n";
 40:     cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
 41: }