Source of partition_copy1a.cpp


  1: //partition_copy1a.cpp

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

  9: int main()
 10: {
 11:     cout << "\nThis program illustrates the use of the STL "
 12:         "partition_copy() algorithm from\n<algorithm> to copy the "
 13:         "odd integers from a vector to one destination and the\neven "
 14:         "integers from the same vector to another destination.";
 15:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 17:     int a[] = {3, 8, 35, 9, 24, 6, 13, 35, 4, 9, 2, 44, 7};
 18:     vector<int> v(begin(a), end(a));
 19:     vector<int> odds(7);
 20:     vector<int> evens(10,1);

 22:     auto iterPair = partition_copy(v.begin(), v.end(),
 23:         odds.begin(), evens.begin(),
 24:         [](int n) {return n%2==1;});

 26:     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
 27:     cout << endl;
 28:     copy(odds.begin(), odds.end(), ostream_iterator<int>(cout, " "));
 29:     cout << endl;
 30:     copy(evens.begin(), evens.end(), ostream_iterator<int>(cout, " "));
 31:     cout << endl;

 33:     if (iterPair.first == odds.end())
 34:         cout << "\nFirst iterator returned points at destination end().";
 35:     else
 36:         cout << "\nFirst iterator returned points at " << *iterPair.first << ".";

 38:     if (iterPair.second == evens.end())
 39:         cout << "\nSecond iterator returned points at destination end().";
 40:     else
 41:         cout << "\nSecond iterator returned points at " << *iterPair.second << ".";
 42:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 43: }