Source of partition_point1a.cpp


  1: //partition_point1a.cpp

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

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL "
 11:         "partition_point() algorithm from\n<algorithm> to determine "
 12:         "the point at which a vector of integers is partitioned\ninto "
 13:         "odd and even integers.";
 14:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 16:     int a[] = {1, 3, 5, 7, 9, 2, 4, 6};
 17:     int b[] = {2, 4, 6, 1, 3, 5, 7, 9};
 18:     int c[] = {1, 3, 5, 7, 9, 11, 13, 15};
 19:     int d[] = {2, 4, 6, 8, 10, 12, 14, 16};
 20:     vector<int> va(a, a+8);
 21:     vector<int> vb(b, b+8);
 22:     vector<int> vc(c, c+8);
 23:     vector<int> vd(d, d+8);

 25:     auto partitionPointIter = partition_point(va.begin(), va.end(),
 26:         [](int n) {return n%2==1;});
 27:     if (partitionPointIter == va.end())
 28:         cout << "\nNo even values found.";
 29:     else
 30:         cout << "\nFirst even value found is " << *partitionPointIter << ".";

 32:     partitionPointIter = partition_point(vb.begin(), vb.end(),
 33:         [](int n) {return n%2==1;});
 34:     if (partitionPointIter == vb.end())
 35:         cout << "\nNo even values found.";
 36:     else
 37:         cout << "\nFirst even value found is " << *partitionPointIter << ".";

 39:     partitionPointIter = partition_point(vc.begin(), vc.end(),
 40:         [](int n) {return n%2==1;});
 41:     if (partitionPointIter == vc.end())
 42:         cout << "\nNo even values found.";
 43:     else
 44:         cout << "\nFirst even value found is " << *partitionPointIter << ".";

 46:     partitionPointIter = partition_point(vd.begin(), vd.end(),
 47:         [](int n) {return n%2==1;});
 48:     if (partitionPointIter == vd.end())
 49:         cout << "\nNo even values found.";
 50:     else
 51:         cout << "\nFirst even value found is " << *partitionPointIter << ".";
 52:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 53: }